如何快速撤消 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:00:54  来源:igfitidea点击:

How to quickly undo staged and unstaged changes in git?

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 addall to index and then stashsaveand drop.

我可以add全部索引然后stashsavedrop

$ 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 --hardand git clean -fd.git reset --hardwill 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 --hardgit clean -fdgit reset --hard将撤消所有暂存更改和未git clean -fd暂存更改(文件和目录)。您可以创建一个别名来执行这两个命令。为此,只需在您的 中添加以下几行.gitconfig

[alias]
  undo = '!git reset --hard && git clean -fd'

回答by Nima

git reset HEAD
git checkout .

git reset HEADwill unstage all changes and git checkout .discards all the changes.

git reset HEAD将取消暂存所有更改并git checkout .丢弃所有更改。

回答by Steven Penny

You can use git clean

你可以使用git clean

$ git status -s
?? oops.txt

$ git clean -f
Removing oops.txt

$ git status -s

More info:

更多信息:

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

通过从当前目录开始递归删除不受版本控制的文件来清理工作树。