git 撤销所有未提交或未保存的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14075581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git undo all uncommitted or unsaved changes
提问by Antarr Byrd
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?
我正在尝试撤消自上次提交以来的所有更改。我试着git reset --hard
和git reset --hard HEAD
观察后,这个职位。我的回答是 head 现在在 18c3773...但是当我查看本地源时,所有文件都还在那里。我错过了什么?
回答by mvp
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 onlygit 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.
这将取消暂存您可能已暂存的所有文件
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
用于预览要删除的文件。
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.
典型用法是在构建脚本中,当您必须确保您的树绝对干净 - 没有任何修改或本地创建的目标文件或构建工件,并且您希望使其工作得非常快并且不重新 -每次都克隆整个存储库。
回答by Abram
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}. If you wish to check first you can run git stash list
to see a list of your stashes. It will look something like:
git stash
创建一个新的 stash,它将成为stash@{0}。如果您想先检查,您可以运行git stash list
查看您的藏品列表。它看起来像:
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.
每个 stash 都以前一个提交消息命名。
回答by keshav
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
- 它“隐藏”了您的本地更改,可以在以后重新应用或在不再需要时删除
more info on stashing
回答by user1872384
回答by Ralph
What I do is
我做的是
git add . (adding everything)
git stash
git stash drop
One liner: git add . && git stash && git stash drop
一个班轮: git add . && git stash && git stash drop
回答by Abdul Rahman K
For those who reached here searching if they could undo git clean -f -d
, by which a file created in eclipsewas 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 中使用“从本地历史还原”来执行相同的操作:从本地历史还原
回答by Zii
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