如何从当前 Git 工作树中删除本地(未跟踪)文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/61212/
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 remove local (untracked) files from the current Git working tree
提问by Readonly
How do you delete untracked local files from your current working tree?
如何从当前工作树中删除未跟踪的本地文件?
回答by
git-clean- Remove untracked files from the working tree
Synopsis
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…?
Description
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.If any optional
<path>...
arguments are given, only those paths are affected.
git-clean- 从工作树中删除未跟踪的文件
概要
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…?
描述
通过从当前目录开始递归删除不受版本控制的文件来清理工作树。
通常,只删除 Git 未知的文件,但如果
-x
指定了该选项,则也会删除忽略的文件。例如,这对于删除所有构建产品很有用。如果
<path>...
给出了任何可选参数,则只有那些路径会受到影响。
Step 1 is to show what will be deleted by using the -n
option:
步骤 1 是使用-n
选项显示将删除的内容:
# Print out the list of files which will be removed (dry run)
git clean -n
Clean Step - beware: this will delete files:
清理步骤 -当心:这将删除文件:
# Delete the files from the repository
git clean -f
- To remove directories, run
git clean -f -d
orgit clean -fd
- To remove ignored files, run
git clean -f -X
orgit clean -fX
- To remove ignored and non-ignored files, run
git clean -f -x
orgit clean -fx
- 要删除目录,请运行
git clean -f -d
或git clean -fd
- 要删除被忽略的文件,请运行
git clean -f -X
或git clean -fX
- 要删除忽略和未忽略的文件,请运行
git clean -f -x
或git clean -fx
Notethe case difference on the X
for the two latter commands.
请注意X
后面两个命令的大小写差异。
If clean.requireForce
is set to "true" (the default) in your configuration, one needs to specify -f
otherwise nothing will actually happen.
如果clean.requireForce
在您的配置中设置为“true”(默认值),则需要指定-f
否则实际上不会发生任何事情。
Again see the git-clean
docs for more information.
再次查看git-clean
文档以获取更多信息。
Options
-f
,--force
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given
-f
,-n
or-i
.
-x
Don't use the standard ignore rules read from .gitignore (per directory) and
$GIT_DIR/info/exclude
, but do still use the ignore rules given with-e
options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.
-X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-n
,--dry-run
Don't actually remove anything, just show what would be done.
-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
,否则 git clean 将拒绝运行。-n
-i
-x
不要使用从 .gitignore(每个目录)和 读取的标准忽略规则
$GIT_DIR/info/exclude
,但仍然使用-e
选项给出的忽略规则。这允许删除所有未跟踪的文件,包括构建产品。这可以用于(可能与 git reset 结合使用)来创建一个原始工作目录来测试干净的构建。
-X
仅删除 Git 忽略的文件。这对于从头开始重建所有内容可能很有用,但保留手动创建的文件。
-n
,--dry-run
实际上不要删除任何内容,只需显示将要执行的操作。
-d
除未跟踪的文件外,还删除未跟踪的目录。如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。
-f
如果您真的想删除这样的目录,请使用选项两次。
回答by robert.berger
Use git clean -f -d
to make sure that directoriesare also removed.
用于git clean -f -d
确保目录也被删除。
Don't actually remove anything, just show what would be done.
git clean -n
or
git clean --dry-run
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 the
-f
option twice if you really want to remove such a directory.git clean -fd
实际上不要删除任何内容,只需显示将要执行的操作。
git clean -n
或者
git clean --dry-run
除未跟踪的文件外,还删除未跟踪的目录。如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。
-f
如果您真的想删除这样的目录,请使用该选项两次。git clean -fd
You can then check if your files are really gone with git status
.
然后,您可以检查您的文件是否真的与git status
.
回答by SystematicFrank
I am surprised nobody mentioned this before:
我很惊讶之前没有人提到过这一点:
git clean -i
That stands for interactiveand you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files. Overall, still faster than running the mandatory --dry-run
before the real cleaning.
这代表交互式,您将快速了解将要删除的内容,从而可以包含/排除受影响的文件。总体来说,还是比--dry-run
在真正的清洗之前强制运行要快。
You will have to toss in a -d
if you also want to take care of empty folders. At the end, it makes for a nice alias:
-d
如果您还想处理空文件夹,则必须折腾。最后,它是一个很好的别名:
git iclean
That being said, the extra hand holding of interactive commands can be tiring for experienced users. These days I just use the already mentioned git clean -fd
话虽如此,对于有经验的用户来说,额外的交互式命令可能会很累。这些天我只是使用已经提到的git clean -fd
回答by Micha? Szajbe
If untracked directory is a git repository of its own (e.g. submodule), you need to use -f
twice:
如果未跟踪的目录是它自己的 git 存储库(例如子模块),则需要使用-f
两次:
git clean -d -f -f
git clean -d -f -f
回答by Thanga
Simple Way to remove untracked files
删除未跟踪文件的简单方法
To remove all untracked files, The simple way is to add all of them firstand reset the repoas below
要删除所有未跟踪文件,最简单的方法是先加他们的一切,并重置回购如下
git add --all
git reset --hard HEAD
回答by hiroshi
I like git stash push -u
because you can undo them all with git stash pop
.
我喜欢,git stash push -u
因为你可以用git stash pop
.
EDIT: Also I found a way to show untracked file in a stash (e.g. git show stash@{0}^3
) https://stackoverflow.com/a/12681856/338986
编辑:我还找到了一种在存储中显示未跟踪文件的方法(例如git show stash@{0}^3
)https://stackoverflow.com/a/12681856/338986
EDIT2: git stash save
is deprecated in favor of push
. Thanks @script-wolf.
EDIT2:git stash save
不赞成使用push
. 谢谢@script-wolf。
回答by Oscar Fraxedas
This is what I always use:
这是我经常使用的:
git clean -fdx
For a very large project you might want to run it a couple of times.
对于非常大的项目,您可能需要运行几次。
回答by Espo
回答by Vijay C
If needed to remove untracked files from particular subdirectory,
如果需要从特定子目录中删除未跟踪的文件,
git clean -f {dir_path}
And combined way to delete untracked dir/files and ignored files.
并结合删除未跟踪的目录/文件和忽略的文件的方法。
git clean -fxd {dir_path}
after this you will have modified files only in git status
.
在此之后,您将只在git status
.