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? 我错过了什么?


#1楼

参考:https://stackoom.com/question/x3hV/git撤消所有未提交或未保存的更改


#2楼

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


#3楼

there is also git stash - which "stashes" your local changes and can be reapplied at a later time or dropped if is no longer required 还有git stash - 它“ git stash ”你的本地更改,可以在以后重新应用,如果不再需要则可以删除

more info on stashing 关于藏匿的更多信息


#4楼

If you wish to " undo " all uncommitted changes simply run: 如果您希望“ 撤消 ”所有未提交的更改,只需运行:

git stash
git stash drop

If you have any untracked files (check by running git status ), these may be removed by running: 如果您有任何未跟踪的文件(通过运行git status检查),可以通过运行以下命令删除这些文件:

git clean -fdx

git stash creates a new stash which will become stash@{0} . git stash创建一个新的存储,它将成为存储@ {0} If you wish to check first you can run git stash list to see a list of your stashes. 如果你想先检查一下,你可以运行git stash list来查看你的stashes列表。 It will look something like: 它看起来像:

stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes

Each stash is named after the previous commit messsage. 每个存储都以先前的提交消息命名。


#5楼

For those who reached here searching if they could undo git clean -f -d , by which a file created in eclipse was deleted, 对于那些到达这里的人,搜索他们是否可以撤消git clean -f -d ,删除在eclipse中创建的文件,

You can do the same from the UI using "restore from local history" for ref: Restore from local history 您可以使用“从本地历史记录还原”从UI执行相同操作,以获取参考: 从本地历史记录还原


#6楼

States transitioning from one commit to new commit 从一个提交转换到新提交的状态

0. last commit,i.e. HEAD commit
1. Working tree changes, file/directory deletion,adding,modification.
2. The changes are staged in index
3. Staged changes are committed

Action for state transitioning 国家过渡的行动

0->1: manual file/directory operation
1->2: git add .
2->3: git commit -m "xxx"

Check diff 检查差异

0->1: git diff
0->2: git diff --cached
0->1, and 0->2: git diff HEAD
last last commit->last commit: git diff HEAD^ HEAD

Revert to last commit 恢复到上次提交

2->1: git reset
1->0: git checkout .     #only for tracked files/directories(actions include modifying/deleting tracked files/directories)
1->0: git clean -fdx     #only for untracked files/directories(action includes adding new files/directories)
2->1, and 1->0: git reset --hard HEAD

Equivalent of git clone, without re-downloading anything 相当于git clone,无需重新下载任何内容

git reset; git checkout .; git clean -fdx
发布了0 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105464374