如何从当前 Git 工作树中删除本地(未跟踪)文件?
如何从当前工作树中删除未跟踪的本地文件?
How do I delete untracked local files from the current working tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何从当前工作树中删除未跟踪的本地文件?
How do I delete untracked local files from the current working tree?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
用户交互方法:
-i 表示交互
-f 表示强制
-d 表示目录
-x 用于忽略文件(如果需要则添加)
注意:添加-n或--dry-run来检查它会做什么。
User interactive approach:
-i for interactive
-f for force
-d for directory
-x for ignored files(add if required)
Note: Add -n or --dry-run to just check what it will do.
我很惊讶之前没有人提到过这一点:
这代表交互式,您将快速了解将要删除的内容,从而可以包含/排除受影响的文件。 总体而言,仍然比真正清洁之前运行强制
--dry-run
更快。如果您还想处理空文件夹,则必须添加
-d
。 最后,它成为了一个很好的别名:话虽如此,对于有经验的用户来说,额外手持交互式命令可能会很累。 这些天我只使用已经提到的 git clean -fd
I am surprised nobody mentioned this before:
That stands for interactive and 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.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: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
这是我经常使用的:
对于一个非常大的项目,您可能需要运行它几次。
This is what I always use:
For a very large project you might want to run it a couple of times.
我喜欢 git stash push -u ,因为你可以使用 git stash pop 撤消它们。
编辑:我还找到了一种显示存储中未跟踪文件的方法(例如
git show stash@{0}^3
)https://stackoverflow.com/a/12681856/338986编辑2:
git stash save
已被弃用,取而代之的是push
。 谢谢@script-wolf。I like
git stash push -u
because you can undo them all withgit 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/338986EDIT2:
git stash save
is deprecated in favor ofpush
. Thanks @script-wolf.运行“git clean”命令时要小心。
始终首先使用
-n
在运行 clean 命令之前始终使用
-n
,因为它会显示哪些文件将被删除。-d
通常,当指定 no 时,git clean 不会递归到未跟踪的目录,以避免删除太多内容。 指定 -d 也可以使其递归到此类目录中。 如果指定了任何路径,则 -d 无关紧要; 所有与指定路径匹配的未跟踪文件(--force 下提到的嵌套 git 目录除外)都将被删除。<代码> -f |
--强制
如果 Git 配置变量 clean.requireForce 未设置为 false,则 git clean 将拒绝删除文件或目录,除非给出 -f 或 -i。 Git 将拒绝修改未跟踪的嵌套 git 存储库(具有 .git 子目录的目录),除非给出第二个 -f 。
现在,如果您打算删除输出,则无需使用
-n
即可运行。默认情况下,
git clean
只会删除未被忽略的未跟踪文件。 任何与 .gitignore 或其他忽略文件中的模式匹配的文件都不会被删除。 如果您也想删除这些文件,可以将-x
添加到 clean 命令中。还可以通过 clean 命令使用交互模式
-i
或者
If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead
在使用
stash --all
之前注意:如果使用
--all
选项,则除了未跟踪的文件之外,还会隐藏并清除忽略的文件。如果使用 --keep-index 选项,则已添加到索引的所有更改都将保持不变。 您的分阶段更改保留在您的工作区中,但同时它们也会保存到您的存储中。
调用不带任何参数的 git stash 相当于 git stash push 。
基于使用的标志的存储可以通过将未暂存/暂存的文件写入存储存储来清除目录。 我使用 stash 和 apply 或 pop 灵活地在任何时间点检索文件。 然后,如果您同意删除隐藏的文件,您可以运行:
要查看有关如何使用隐藏的完整说明,请参阅此 如何在 git 中按名称命名和检索存储?
Be careful while running `git clean` command.
Always use
-n
firstAlways use
-n
before running the clean command as it will show you what files would get removed.-d
Normally, when no is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under --force) will be removed.-f
|--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 or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.
Now run without
-n
if output was what you intend to remove.By default,
git clean
will only remove untracked files that are not ignored. Any file that matches a pattern in your .gitignore or other ignore files will not be removed. If you want to remove those files too, you can add a-x
to the clean command.There is also interactive mode available
-i
with the clean commandAlternatively
If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead
Before you use
stash --all
note:If the
--all
option is used, then the ignored files are stashed and cleaned in addition to the untracked files.If the --keep-index option is used, all changes already added to the index are left intact. Your staged changes remain in your workspace, but at the same time, they are also saved into your stash.
Calling
git stash
without any arguments is equivalent togit stash push
.Stashing based on the used flags can clear your directory from unstaged / staged files by writing them to stash storage. I give’s flexibility to retrieve the files at any point in time using stash with apply or pop. Then if you are fine with removing your stashed files you could run:
To see full instruction on how to work with stash see this How to name and retrieve a stash by name in git?
更好的方法是使用: git clean
这会删除未跟踪的文件,包括目录
(-d)
和被git (-x)
忽略的文件。另外,将
-f
参数替换为-n
以执行dry-run
或-i
进行交互模式它会告诉你什么将被删除。A better way is to use: git clean
This removes untracked files, including directories
(-d)
and files ignored bygit (-x)
.Also, replace the
-f
argument with-n
to perform adry-run
or-i
for interactive mode and it will tell you what will be removed.要删除未跟踪的文件:
To remove Untracked files :
我刚刚发明并尝试过针对这种情况的生活窍门(效果很好):
当心!请务必提交任何所需的更改(即使在非未跟踪的文件中)在执行此操作之前。
A lifehack for such situation I just invented and tried (that works perfectly):
Beware! Be sure to commit any needed changes (even in non-untracked files) before performing this.
git clean -fd
删除目录git clean -fX
删除被忽略的文件git clean -fx
删除被忽略和未忽略的文件以上均可使用选项组合为
git clean -fdXx
检查 git 手册以获取更多帮助
git clean -fd
removes directorygit clean -fX
removes ignored filesgit clean -fx
removes ignored and un-ignored filescan be used all above options in combination as
git clean -fdXx
check git manual for more help
要在实际删除之前了解要删除的内容:
git clean -d -n
它将输出类似以下内容的内容:
Would remove example.txt
要删除输出中列出的所有内容上一个命令:
git clean -d -f
它将输出类似:
删除sample.txt
To know what will be deleted before actually deleting:
git clean -d -n
It will output something like:
Would remove sample.txt
To delete everything listed in the output of the previous command:
git clean -d -f
It will output something like:
Removing sample.txt
要删除未跟踪的文件,您应该首先使用命令查看将受到清理影响的文件,
这将显示将被删除的文件列表。 现在要实际删除这些文件,请使用以下命令:
To remove the untracked files you should first use command to view the files that will be affected by cleaning
This will show you the list of files that will be deleted. Now to actually delete those files use this command:
就是这样。
了解更多信息
https://www.atlassian.com/git /tutorials/ saving-changes/git-stash#stashing-your-work
This is the way.
For more information
https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work
从 git docs 中删除未跟踪文件的建议命令是 git clean
git clean - 从工作树中删除未跟踪文件
建议方法:< /strong> 使用 git clean -i 的交互模式
这样我们就可以控制它。 让我们看看剩余的可用选项。
可用选项:
说明:
1. -d
除了未跟踪的文件之外,还删除未跟踪的目录。 如果未跟踪的目录由不同的 Git 存储库管理,
默认情况下不会删除它。 如果您确实想删除这样的目录,请使用 -f 选项两次。
2. -f, --force
如果 Git 配置变量 clean.requireForce 未设置为 false,git clean 将拒绝运行,除非给出 -f、-n 或
-我。
3. -i, --interactive
显示要执行的操作并以交互方式清理文件。 详细信息请参见“交互模式”。
4. -n, --dry-run
实际上不删除任何内容,只是显示将要执行的操作。
5. -q, --quiet
保持安静,只报告错误,而不报告成功删除的文件。
6. -e , --exclude=
除了 .gitignore (每个目录)和 $GIT_DIR/info/exclude 中找到的模式外,还应考虑这些模式位于
一组有效的忽略规则。
7. -x
不要使用从 .gitignore (每个目录)和 $GIT_DIR/info/exclude 读取的标准忽略规则,但仍然使用忽略
使用 -e 选项给出的规则。 这允许删除所有未跟踪的文件,包括构建产品。 这可以使用(可能在
与 git reset 结合)创建一个原始工作目录来测试干净的构建。
8. -X
仅删除被 Git 忽略的文件。 这对于从头开始重建所有内容可能很有用,但保留手动创建的文件。
uggested Command for Removing Untracked Files from git docs is git clean
git clean - Remove untracked files from the working tree
Suggested Method: Interative Mode by using
git clean -i
so we can have control over it. let see remaining available options.
Available Options:
Explanation:
1. -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.
2. -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.
3. -i, --interactive
Show what would be done and clean files interactively. See “Interactive mode” for details.
4. -n, --dry-run
Don’t actually remove anything, just show what would be done.
5. -q, --quiet
Be quiet, only report errors, but not the files that are successfully removed.
6. -e , --exclude=
In addition to those found in .gitignore (per directory) and $GIT_DIR/info/exclude, also consider these patterns to be in the
set of the ignore rules in effect.
7. -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.
8. -X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
git clean -f 从工作目录中删除未跟踪的文件。
我在我的博客中介绍了一些基础知识,git-intro-basic-commands
git clean -f to remove untracked files from working directory.
I have covered some basics here in my blog, git-intro-basic-commands
正常的 git clean 命令不会使用我的 git version 2.9.0.windows.1 删除未跟踪的文件。
Normal
git clean
command doesn't remove untracked files with mygit version 2.9.0.windows.1
.我们可以使用下面的 git 注释轻松地从当前 git 工作树中删除本地未跟踪的文件。
示例:
链接:
We can easily removed local untracked files from the current git working tree by using below git comments.
Example:
Links :
如果您只想删除“git status”列出的未跟踪的文件,
我更喜欢这个而不是“git clean”,因为“git clean”会删除文件
被 git 忽略,所以你的下一个构建将不得不重建所有内容
并且您也可能会丢失 IDE 设置。
If you just want to delete the files listed as untracked by 'git status'
I prefer this to 'git clean' because 'git clean' will delete files
ignored by git, so your next build will have to rebuild everything
and you may lose your IDE settings too.
git add --all
、git stash
和git stash drop
,按此顺序尝试这三个命令,以便删除所有未跟踪的文件。 通过将所有这些未跟踪的文件添加到 git 并存储它们,会将所有这些未跟踪的文件移动到存储列表并删除顶部的文件,即 stash@{0} 将从存储列表中删除存储的更改。git add --all
,git stash
andgit stash drop
, try these three commands in this order inorder to remove all untracked files. By adding all those untracked files to git and stashing them will move all those untracked files to stash list and dropping out top one i.e., stash@{0} will remove the stashed changes from stash list.对我来说,只有以下工作有效:
在所有其他情况下,我收到某些子目录的消息“正在跳过目录”。
For me only following worked:
In all other cases, I was getting message "Skipping Directory" for some subdirectories.
git clean -f -d -x $(git rev-parse --show-cdup) 将 clean 应用于根目录,无论您在存储库目录树中的何处调用它。 我一直使用它,因为它不会强迫您离开您现在工作的文件夹,并且允许清理和删除文件。 从你所在的地方就可以做出承诺。
确保标志
-f
、-d
、-x
符合您的需求:还有其他标志可用,只需检查
git clean --help
。git clean -f -d -x $(git rev-parse --show-cdup)
applies clean to the root directory, no matter where you call it within a repository directory tree. I use it all the time as it does not force you to leave the folder where you working now and allows to clean & commit right from the place where you are.Be sure that flags
-f
,-d
,-x
match your needs:There are other flags as well available, just check
git clean --help
.git-clean
- 从工作树中删除未跟踪的文件git-clean
- Remove untracked files from the working tree删除未跟踪文件的简单方法
要删除所有未跟踪文件,简单
方法是首先添加所有内容并重置存储库,如下所示
Simple Way to remove untracked files
To remove all untracked files, The simple
way is to add all of them first and reset the repo as below
如果未跟踪的目录是自己的git存储库(例如子模块),则需要使用
-f
两次:git clean -d -f -f
If untracked directory is a git repository of its own (e.g. submodule), you need to use
-f
twice:git clean -d -f -f
使用 git clean -f -d 来确保目录也被删除。
实际上并不删除任何内容,只是显示将要执行的操作。
<前><代码>git clean -n
或
除了未跟踪的文件之外,还删除未跟踪的目录。 如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。 如果您确实想删除这样的目录,请使用
-f
选项两次。然后,您可以使用
git status
检查您的文件是否真的消失了。Use
git clean -f -d
to make sure that directories are also removed.Don’t actually remove anything, just show what would be done.
or
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.You can then check if your files are really gone with
git status
.第 1 步是使用
-n
选项显示将删除的内容:清理步骤 - 注意:这将删除文件:
git clean -f -d
或git clean -fd
git clean -f -X
或git clean -fX
>git clean -f -x
或git clean -fx
注意 < code>X 用于后两个命令。
如果在配置中将
clean.requireForce
设置为“true”(默认值),则需要指定-f
,否则实际上不会发生任何情况。再次参阅
git-clean
文档以获取更多信息。Step 1 is to show what will be deleted by using the
-n
option:Clean Step - beware: this will delete files:
git clean -f -d
orgit clean -fd
git clean -f -X
orgit clean -fX
git clean -f -x
orgit clean -fx
Note the case difference on the
X
for the two latter commands.If
clean.requireForce
is set to "true" (the default) in your configuration, one needs to specify-f
otherwise nothing will actually happen.Again see the
git-clean
docs for more information.如果需要从特定子目录中删除未跟踪的文件,
并结合方式删除未跟踪的目录/文件和忽略的文件。
此后,您将仅在
git status
中修改文件。If needed to remove untracked files from particular subdirectory,
And combined way to delete untracked dir/files and ignored files.
after this you will have modified files only in
git status
.删除此存储库+子模块中的所有额外文件夹和文件
这将使您处于与新克隆相同的状态。
删除此存储库中的所有额外文件夹和文件,但不删除其子模块
删除额外的文件夹,但不删除文件(例如构建或日志文件夹)
删除额外的文件夹+忽略的文件(但不是新添加的文件)
如果文件未被忽略且尚未签入,则它将保留。 注意大写的X。
新的交互模式
Remove all extra folders and files in this repo + submodules
This gets you in same state as fresh clone.
Remove all extra folders and files in this repo but not its submodules
Remove extra folders but not files (ex. build or logs folder)
Remove extra folders + ignored files (but not newly added files)
If file wasn't ignored and not yet checked-in then it stays. Note the capital X.
New interactive mode
好的,删除不需要的未跟踪文件和文件夹很容易在命令行中使用
git
,只需这样做:双重检查
在这种情况下,
-f
代表强制,-d
代表目录...因此,如果您只想删除文件,则只能使用
-f
:如果您想删除(目录)和文件,则只能删除未跟踪的像这样的目录和文件:
此外,您可以使用
-x
标志来包含 git 忽略的文件。 如果您想删除所有内容,这会很有帮助。并添加
-i
标志,使 git 询问您是否有权在旅途中一一删除文件。如果您不确定并想先检查一下,请添加
-n
标志。如果您不想在成功删除后看到任何报告,请使用
-q
。我还创建了下面的图像以使其更令人难忘,特别是我看到很多人有时混淆
-f
来清理文件夹或以某种方式将其混淆!OK, deleting unwanted untracked files and folders are easy using
git
in command line, just do it like this:Double check before doing it as it will delete the files and folders without making any history...
Also in this case,
-f
stands for force and-d
stands for directory...So, if you want to delete files only, you can use
-f
only:If you want to delete(directories) and files, you can delete only untracked directories and files like this:
Also, you can use
-x
flag for including the files which are ignored by git. This would be helpful if you want to delete everything.And adding
-i
flag, makes git asking you for permission for deleting files one by one on the go.If you not sure and want to check things first, add
-n
flag.Use
-q
if you don't want to see any report after successful deletion.I also create the image below to make it more memorable, especially I have seen many people confuse
-f
for cleaning folder sometimes or mix it up somehow!git-clean 是您正在寻找的。 它用于从工作树中删除未跟踪的文件。
git-clean is what you are looking for. It is used to remove untracked files from the working tree.