Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

git - git撤消所有未提交或未保存的更改(git undo all uncommitted or unsaved changes)

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 .

(在查看这篇文章后,我尝试了git reset --hardgit reset --hard HEAD 。)

I responds with head is now at 18c3773... but when I look at my local source all the files are still there.

(我用头响应现在是18c3773 ...但是当我查看我的本地源时,所有文件仍然存在。)

What am I missing?

(我错过了什么?)

  ask by Antarr Byrd translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • This will unstage all files you might have staged with git add :

    (这将取消您使用git add暂存的所有文件:)

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

    (这将还原所有本地未提交的更改(应在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跟踪文件:)

     git clean -fdx 

    WARNING: -x will also remove all ignored files, including ones specified by .gitignore !

    (警告: -x还将删除所有被忽略的文件,包括.gitignore指定的文件!)

    You may want to use -n for preview of files to be deleted.

    (您可能希望使用-n来预览要删除的文件。)


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 clone (但它不会重新下载任何内容,因此速度更快):)

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.

(这种情况的典型用法是在构建脚本中,当你必须确保你的树是绝对干净的 - 没有任何修改或本地创建的目标文件或构建工件,并且你想让它工作得非常快而且不能重新每次都克隆整个存储库。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...