git:撤消所有工作目录更改,包括新文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1090309/
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 working dir changes including new files
提问by Aler
How to delete all changes from working directory including new untracked files. I know that git checkout -f
does that, but it doesn't delete new untracked files created since last commit.
如何从工作目录中删除所有更改,包括新的未跟踪文件。我知道这样git checkout -f
做,但它不会删除自上次提交以来创建的新的未跟踪文件。
Does anybody have an idea how to do that?
有没有人知道如何做到这一点?
回答by
git reset --hard # removes staged and working directory changes
## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)
To see what will be deleted before-hand, without actually deleting it, use the -n
flag (this is basically a test-run). When you are ready to actually delete, then remove the -n
flag:
要事先查看将删除的内容,而不是实际删除它,请使用该-n
标志(这基本上是一个测试运行)。当您准备好实际删除时,请删除-n
标志:
git clean -nfd
git clean -nfd
回答by Heath Dutton
Safest method, which I use frequently:
最安全的方法,我经常使用:
git clean -fd
Syntax explanation as per /docs/git-clean
page:
/docs/git-clean
每页的语法解释:
-f
(alias:--force
). If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.-d
. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.
-f
(别名:)--force
。如果 Git 配置变量 clean.requireForce 没有设置为 false,除非给定 -f、-n 或 -i,否则 git clean 将拒绝删除文件或目录。Git 将拒绝删除带有 .git 子目录或文件的目录,除非给出第二个 -f。-d
. 除未跟踪的文件外,还删除未跟踪的目录。如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。如果您真的想删除这样的目录,请使用 -f 选项两次。
As mentioned in the comments, it might be preferable to do a git clean -nd
which does a dry run and tells you what would be deleted before actually deleting it.
正如评论中提到的,最好先做一个git clean -nd
试运行并告诉你在实际删除之前会删除什么。
Link to git clean
doc page:
https://git-scm.com/docs/git-clean
链接到git clean
文档页面:https:
//git-scm.com/docs/git-clean
回答by jfountain
回答by Greg Hewgill
Have a look at the git clean
command.
看看git clean
命令。
git-clean - Remove untracked files from the working tree
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
git-clean - 从工作树中删除未跟踪的文件
通过从当前目录开始递归删除不受版本控制的文件来清理工作树。
通常,只删除 git 未知的文件,但如果指定了 -x 选项,也会删除忽略的文件。例如,这对于删除所有构建产品很有用。
回答by donquixote
The following works:
以下工作:
git add -A .
git stash
git stash drop stash@{0}
Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.
请注意,这将丢弃您未暂存和暂存的本地更改。因此,在运行这些命令之前,您应该提交您想要保留的任何内容。
A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.
一个典型的用例:您移动了很多文件或目录,然后想要回到原始状态。
回答by Gerard de Visser
You can do this in two steps:
您可以分两步执行此操作:
- Revert modified files:
git checkout -f
- Remove untracked files:
git clean -fd
- 还原修改后的文件:
git checkout -f
- 删除未跟踪的文件:
git clean -fd
回答by skube
I thought it was (warning: following willwipe out everything)
我认为是(警告:以下将消灭一切)
$ git reset --hard HEAD
$ git clean -fd
The reset
to undo changes. The clean
to remove any untracked files and directories.
该reset
撤消更改。的clean
以除去任何未跟踪˚F尔斯和directories。
回答by santosh kumar
git reset --hard origin/{branchName}
It will delete all untracked files.
它将删除所有未跟踪的文件。
回答by Taiga
git clean -i
will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.
git clean -i
将首先向您显示要删除的项目,并在您确认后继续。我发现这在处理不应被意外删除的重要文件时很有用。
See git help clean
for more information, including some other useful options.
有关git help clean
更多信息,包括一些其他有用的选项,请参阅。
回答by Agorreca
If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig
. For instance:
如果要放弃所有更改,可以在.gitconfig
. 例如:
[alias]
discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"
Usage: git discard
用法: git discard