如何快速撤消 git 中已暂存和未暂存的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16160199/
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
How to quickly undo staged and unstaged changes in git?
提问by hIpPy
What's the quickest way to undo changes (staged and unstaged) in Git?
在 Git 中撤消更改(暂存和未暂存)的最快方法是什么?
Both files unstaged.
两个文件都未暂存。
$ git status -s
M file1.txt # unstaged
?? oops.txt # unstaged
One file staged, one file unstaged.
一份文件暂存,一份文件未暂存。
$ git status -s
M file1.txt # staged
?? oops.txt # unstaged
I can add
all to index and then stash
save
and drop
.
我可以add
全部索引然后stash
save
和drop
。
$ git add .
$ git stash save
$ git stash drop
$ git status
nothing to commit, working directory clean
Is there a quicker way?
有没有更快的方法?
回答by William Seiti Mizuta
You need to use two commands: git reset --hard
and git clean -fd
.git reset --hard
will undo all staged changes and git clean -fd
, unstaged changes (files and directories). You can create a alias that will do the two commands. For that, just add the following lines in your .gitconfig
:
您需要使用两个命令:git reset --hard
和git clean -fd
。git reset --hard
将撤消所有暂存更改和未git clean -fd
暂存更改(文件和目录)。您可以创建一个别名来执行这两个命令。为此,只需在您的 中添加以下几行.gitconfig
:
[alias]
undo = '!git reset --hard && git clean -fd'
回答by Nima
git reset HEAD
git checkout .
git reset HEAD
will unstage all changes and git checkout .
discards all the changes.
git reset HEAD
将取消暂存所有更改并git checkout .
丢弃所有更改。