如何从当前 Git 工作树中删除本地(未跟踪)文件?

发布于 2024-07-04 10:08:26 字数 27 浏览 9 评论 0原文

如何从当前工作树中删除未跟踪的本地文件?

How do I delete untracked local files from the current working tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

抠脚大汉 2024-07-11 10:08:26

用户交互方法:

git clean -i -fd

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/arsdumpgenerator/s3/ [y/N]? y

-i 表示交互
-f 表示强制
-d 表示目录
-x 用于忽略文件(如果需要则添加)

注意:添加-n--dry-run来检查它会做什么。

User interactive approach:

git clean -i -fd

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/arsdumpgenerator/s3/ [y/N]? y

-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.

撩心不撩汉 2024-07-11 10:08:26

我很惊讶之前没有人提到过这一点:

git clean -i

这代表交互式,您将快速了解将要删除的内容,从而可以包含/排除受影响的文件。 总体而言,仍然比真正清洁之前运行强制 --dry-run 更快。

如果您还想处理空文件夹,则必须添加 -d 。 最后,它成为了一个很好的别名:

git iclean

话虽如此,对于有经验的用户来说,额外手持交互式命令可能会很累。 这些天我只使用已经提到的 git clean -fd

I am surprised nobody mentioned this before:

git clean -i

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:

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

远昼 2024-07-11 10:08:26

这是我经常使用的:

git clean -fdx

对于一个非常大的项目,您可能需要运行它几次。

This is what I always use:

git clean -fdx

For a very large project you might want to run it a couple of times.

-小熊_ 2024-07-11 10:08:26

我喜欢 git stash push -u ,因为你可以使用 git stash pop 撤消它们。

编辑:我还找到了一种显示存储中未跟踪文件的方法(例如 git show stash@{0}^3https://stackoverflow.com/a/12681856/338986

编辑2:git stash save已被弃用,取而代之的是push。 谢谢@script-wolf。

I like git stash push -u because you can undo them all with 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

EDIT2: git stash save is deprecated in favor of push. Thanks @script-wolf.

少女净妖师 2024-07-11 10:08:26

运行“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 。

git clean -n -d 
git clean -n -d -f

现在,如果您打算删除输出,则无需使用 -n 即可运行。

git clean -d -f

默认情况下,git clean只会删除未被忽略的未跟踪文件。 任何与 .gitignore 或其他忽略文件中的模式匹配的文件都不会被删除。 如果您也想删除这些文件,可以将 -x 添加到 clean 命令中。

git clean -f -d -x

还可以通过 clean 命令使用交互模式 -i

git clean -x -i

或者

If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead

git stash --all

在使用 stash --all 之前注意:
如果使用 --all 选项,则除了未跟踪的文件之外,还会隐藏并清除忽略的文件

git stash push --keep-index

如果使用 --keep-index 选项,则已添加到索引的所有更改都将保持不变。 您的分阶段更改保留在您的工作区中,但同时它们也会保存到您的存储中。

调用不带任何参数的 git stash 相当于 git stash push 。

git stash push -m "name your stash" // before git stash save (deprecated)

基于使用的标志的存储可以通过将未暂存/暂存的文件写入存储存储来清除目录。 我使用 stashapplypop 灵活地在任何时间点检索文件。 然后,如果您同意删除隐藏的文件,您可以运行:

git stash drop // or clean

要查看有关如何使用隐藏的完整说明,请参阅此 如何在 git 中按名称命名和检索存储?

在此处输入图像描述

Be careful while running `git clean` command.

Always use -n first

Always 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.

git clean -n -d 
git clean -n -d -f

Now run without -n if output was what you intend to remove.

git clean -d -f

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.

git clean -f -d -x

There is also interactive mode available -i with the clean command

git clean -x -i

Alternatively

If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead

git stash --all

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.

git stash push --keep-index

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 to git stash push.

git stash push -m "name your stash" // before git stash save (deprecated)

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:

git stash drop // or clean

To see full instruction on how to work with stash see this How to name and retrieve a stash by name in git?

enter image description here

紙鸢 2024-07-11 10:08:26

更好的方法是使用: git clean

git clean -d -x -f

这会删除未跟踪的文件,包括目录 (-d) 和被 git (-x) 忽略的文件。

另外,将 -f 参数替换为 -n 以执行 dry-run-i 进行交互模式它会告诉你什么将被删除。

A better way is to use: git clean

git clean -d -x -f

This removes untracked files, including directories (-d) and files ignored by git (-x).

Also, replace the -f argument with -n to perform a dry-run or -i for interactive mode and it will tell you what will be removed.

始终不够 2024-07-11 10:08:26

要删除未跟踪的文件:

git add .
git reset --hard HEAD

To remove Untracked files :

git add .
git reset --hard HEAD
明明#如月 2024-07-11 10:08:26

我刚刚发明并尝试过针对这种情况的生活窍门(效果很好):

git add .
git reset --hard HEAD

当心!请务必提交任何所需的更改(即使在非未跟踪的文件中)在执行此操作之前

A lifehack for such situation I just invented and tried (that works perfectly):

git add .
git reset --hard HEAD

Beware! Be sure to commit any needed changes (even in non-untracked files) before performing this.

小耗子 2024-07-11 10:08:26

git clean -fd 删除目录

git clean -fX 删除被忽略的文件

git clean -fx 删除被忽略和未忽略的文件

以上均可使用选项组合为

git clean -fdXx

检查 git 手册以获取更多帮助

git clean -fd removes directory

git clean -fX removes ignored files

git clean -fx removes ignored and un-ignored files

can be used all above options in combination as

git clean -fdXx

check git manual for more help

小瓶盖 2024-07-11 10:08:26

要在实际删除之前了解要删除的内容:

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

深巷少女 2024-07-11 10:08:26

要删除未跟踪的文件,您应该首先使用命令查看将受到清理影响的文件,

git clean -fdn

这将显示将被删除的文件列表。 现在要实际删除这些文件,请使用以下命令:

git clean -fd

To remove the untracked files you should first use command to view the files that will be affected by cleaning

git clean -fdn

This will show you the list of files that will be deleted. Now to actually delete those files use this command:

git clean -fd
So尛奶瓶 2024-07-11 10:08:26

从 git docs 中删除未跟踪文件的建议命令是 git clean

git clean - 从工作树中删除未跟踪文件

建议方法:< /strong> 使用 git clean -i 的交互模式
这样我们就可以控制它。 让我们看看剩余的可用选项。

可用选项:

git clean 
    -d -f -i -n -q -e -x -X (can use either)

说明:

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:

git clean 
    -d -f -i -n -q -e -x -X (can use either)

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.

ゞ花落谁相伴 2024-07-11 10:08:26

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

幼儿园老大 2024-07-11 10:08:26

正常的 git clean 命令不会使用我的 git version 2.9.0.windows.1 删除未跟踪的文件。

$ git clean -fdx     # doesn't remove untracked files
$ git clean -fdx *   # Append star then it works!

Normal git clean command doesn't remove untracked files with my git version 2.9.0.windows.1.

$ git clean -fdx     # doesn't remove untracked files
$ git clean -fdx *   # Append star then it works!
千笙结 2024-07-11 10:08:26

我们可以使用下面的 git 注释轻松地从当前 git 工作树中删除本地未跟踪的文件。

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

示例:

git reset --hard HEAD

链接:

  1. https://git -scm.com/docs/git-reset
  2. 如何使用“git reset --hard HEAD”恢复到之前的提交?
  3. 将本地存储库分支重置为就像远程存储库 HEAD
  4. https://jwiegley.github.io/git-from-the -bottom-up/3-Reset/4-doing-a-hard-reset.html

We can easily removed local untracked files from the current git working tree by using below git comments.

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

Example:

git reset --hard HEAD

Links :

  1. https://git-scm.com/docs/git-reset
  2. How do I use 'git reset --hard HEAD' to revert to a previous commit?
  3. Reset local repository branch to be just like remote repository HEAD
  4. https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html
℡寂寞咖啡 2024-07-11 10:08:26

递归地清理 git 存储库和所有子模块

以下命令将清除
当前 git 存储库及其所有子模块递归:

(git clean -d -x -f && git submodule foreach --recursive git clean -d -x -f) 
  

Clean out git repository and all submodules recursively

The following command will clean out
the current git repository and all its submodules recursively:

(git clean -d -x -f && git submodule foreach --recursive git clean -d -x -f)
薔薇婲 2024-07-11 10:08:26

如果您只想删除“git status”列出的未跟踪的文件,

git stash save -u
git stash drop "stash@{0}"

我更喜欢这个而不是“git clean”,因为“git clean”会删除文件
被 git 忽略,所以你的下一个构建将不得不重建所有内容
并且您也可能会丢失 IDE 设置。

If you just want to delete the files listed as untracked by 'git status'

git stash save -u
git stash drop "stash@{0}"

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.

迷鸟归林 2024-07-11 10:08:26

git add --allgit stashgit stash drop,按此顺序尝试这三个命令,以便删除所有未跟踪的文件。 通过将所有这些未跟踪的文件添加到 git 并存储它们,会将所有这些未跟踪的文件移动到存储列表并删除顶部的文件,即 stash@{0} 将从存储列表中删除存储的更改。

git add --all, git stash and git 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.

掩饰不了的爱 2024-07-11 10:08:26

对我来说,只有以下工作有效:

git clean -ffdx

在所有其他情况下,我收到某些子目录的消息“正在跳过目录”。

For me only following worked:

git clean -ffdx

In all other cases, I was getting message "Skipping Directory" for some subdirectories.

花想c 2024-07-11 10:08:26

git clean -f -d -x $(git rev-parse --show-cdup) 将 clean 应用于根目录,无论您在存储库目录树中的何处调用它。 我一直使用它,因为它不会强迫您离开您现在工作的文件夹,并且允许清理和删除文件。 从你所在的地方就可以做出承诺。

确保标志 -f-d-x 符合您的需求:

-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
       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. This affects
       also git submodules where the storage area of the removed submodule
       under .git/modules/ is not removed until -f is given twice.

-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.

还有其他标志可用,只需检查 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:

-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
       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. This affects
       also git submodules where the storage area of the removed submodule
       under .git/modules/ is not removed until -f is given twice.

-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.

There are other flags as well available, just check git clean --help.

回心转意 2024-07-11 10:08:26

删除未跟踪文件的简单方法

要删除所有未跟踪文件,简单
方法是首先添加所有内容重置存储库,如下所示

git add --all
git reset --hard HEAD

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 add --all
git reset --hard HEAD

此刻的回忆 2024-07-11 10:08:26

如果未跟踪的目录是自己的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

獨角戲 2024-07-11 10:08:26

使用 git clean -f -d 来确保目录也被删除。

  1. 实际上并不删除任何内容,只是显示将要执行的操作。

    <前><代码>git clean -n

    git clean --dry-run 
      
  2. 除了未跟踪的文件之外,还删除未跟踪的目录。 如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。 如果您确实想删除这样的目录,请使用 -f 选项两次。

    git clean -fd 
      

然后,您可以使用 git status 检查您的文件是否真的消失了。

Use git clean -f -d to make sure that directories are also removed.

  1. Don’t actually remove anything, just show what would be done.

    git clean -n
    

    or

    git clean --dry-run
    
  2. 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
    

You can then check if your files are really gone with git status.

我偏爱纯白色 2024-07-11 10:08:26

git-clean - 从工作树中删除未跟踪的文件

概要

git clean [-d] [-f] [-i] [-n] [-q] [-e <模式>] [-x |   -X] [--] <路径>...  
  

描述

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

通常,只会删除 Git 未知的文件,但如果指定了 -x 选项,则忽略的文件也会被删除。 例如,这对于删除所有构建产品很有用。

如果给出任何可选的...参数,则只有这些路径受到影响。


第 1 步是使用 -n 选项显示将删除的内容:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

清理步骤 - 注意:这将删除文件

# Delete the files from the repository
git clean -f
  • 要删除目录,请运行 git clean -f -dgit clean -fd
  • 要删除忽略的文件,请运行 git clean -f -Xgit clean -fX >
  • 要删除忽略和未忽略的文件,请运行 git clean -f -xgit clean -fx

注意 < code>X 用于后两个命令。

如果在配置中将 clean.requireForce 设置为“true”(默认值),则需要指定 -f,否则实际上不会发生任何情况。

再次参阅 git-clean 文档以获取更多信息。


选项

-f--force

如果 Git 配置变量 clean.requireForce 未设置为
false,除非给出 -f-n-i,否则 git clean 将拒绝运行。

<代码>-x

不要使用从 .gitignore 读取的标准忽略规则(根据
目录)和 $GIT_DIR/info/exclude,但仍然使用忽略
使用 -e 选项给出的规则。 这允许删除所有未跟踪的文件,
包括构建产品。 这可以使用(可能结合
使用 git Reset)创建一个原始工作目录来测试干净的
构建。

<代码>-X

仅删除被 Git 忽略的文件。 这可能对重建有用
一切从头开始,但保留手动创建的文件。

-n--dry-run

实际上并不删除任何内容,只是显示将要执行的操作。

-d

除了未跟踪的文件外,还删除未跟踪的目录。 如果
untracked 目录由不同的 Git 存储库管理,它是
默认情况下不删除。 如果您确实想要,请使用 -f 选项两次
删除这样的目录。

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.


Step 1 is to show what will be deleted by using the -n option:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

Clean Step - beware: this will delete files:

# Delete the files from the repository
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git 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.


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.

最丧也最甜 2024-07-11 10:08:26

如果需要从特定子目录中删除未跟踪的文件,

git clean -f {dir_path}

并结合方式删除未跟踪的目录/文件和忽略的文件。

git clean -fxd {dir_path}

此后,您将仅在 git status 中修改文件。

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.

戏蝶舞 2024-07-11 10:08:26

删除此存储库+子模块中的所有额外文件夹和文件

这将使您处于与新克隆相同的状态。

git clean -ffdx

删除此存储库中的所有额外文件夹和文件,但不删除其子模块

git clean -fdx

删除额外的文件夹,但不删除文件(例如构建或日志文件夹)

git clean -fd

删除额外的文件夹+忽略的文件(但不是新添加的文件)

如果文件未被忽略且尚未签入,则它将保留。 注意大写的X。

git clean -fdX

新的交互模式

git clean

Remove all extra folders and files in this repo + submodules

This gets you in same state as fresh clone.

git clean -ffdx

Remove all extra folders and files in this repo but not its submodules

git clean -fdx

Remove extra folders but not files (ex. build or logs folder)

git clean -fd

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.

git clean -fdX

New interactive mode

git clean
旧人哭 2024-07-11 10:08:26

好的,删除不需要的未跟踪文件和文件夹很容易在命令行中使用git,只需这样做:

git clean -fd

双重检查

在这种情况下,-f 代表强制,-d 代表目录...

因此,如果您只想删除文件,则只能使用 -f

git clean -f

如果您想删除(目录)和文件,则只能删除未跟踪的像这样的目录和文件:

git clean -fd

此外,您可以使用 -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:

git clean -fd

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:

git clean -f

If you want to delete(directories) and files, you can delete only untracked directories and files like this:

git clean -fd

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!

deleting unwanted untracked files and folder

说不完的你爱 2024-07-11 10:08:26

git-clean 是您正在寻找的。 它用于从工作树中删除未跟踪的文件。

git-clean is what you are looking for. It is used to remove untracked files from the working tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文