git 撤销所有未提交或未保存的更改 [英] git undo all uncommitted or unsaved changes

查看:45
本文介绍了git 撤销所有未提交或未保存的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试撤销自上次提交以来的所有更改.我在查看 这篇文章.我的回答是 head 现在在 18c3773...但是当我查看本地源时,所有文件都还在那里.我错过了什么?

解决方案

  • 这将取消暂存您可能使用 git add 暂存的所有文件:

    git reset

  • 这将还原所有本地未提交的更改(应在 repo root 中执行):

    git checkout .

    您还可以仅将未提交的更改还原到特定文件或目录:

    git checkout [some_dir|file.txt]

    另一种还原所有未提交更改的方法(输入时间更长,但适用于任何子目录):

    git reset --hard HEAD

  • 这将删除所有本地未跟踪的文件,因此 git 跟踪的文件保留:

    git clean -fdx

    <块引用>

    警告: -x 还将删除所有被忽略的文件,包括由 .gitignore 指定的文件!您可能需要使用 -n 来预览要删除的文件.

<小时>

总结一下:执行下面的命令基本上等同于从原始来源新鲜的git clone(但它不会重新下载任何东西,因此速度要快得多):

git resetgit 结帐.git clean -fdx

这的典型用法是在构建脚本中,当您必须确保您的树绝对干净 - 没有任何修改或本地创建的目标文件或构建工件,并且您希望使其工作得非常快并且不要每次都重新克隆整个存储库.

I'm trying to undo all changes since my last commit. I tried git reset --hard and git reset --hard HEAD after viewing this post. I responds with head is now at 18c3773... but when I look at my local source all the files are still there. What am I missing?

解决方案

  • This will unstage all files you might have staged with git add:

    git reset
    

  • This will revert all local uncommitted changes (should be executed in repo root):

    git checkout .
    

    You can also revert uncommitted changes only to particular file or directory:

    git checkout [some_dir|file.txt]
    

    Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):

    git reset --hard HEAD
    

  • This will remove all local untracked files, so only git tracked files remain:

    git clean -fdx
    

    WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.


To sum it up: executing commands below is basically equivalent to fresh git clone from original source (but it does not re-download anything, so is much faster):

git reset
git checkout .
git clean -fdx

Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.

这篇关于git 撤销所有未提交或未保存的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆