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


当前回答

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

git clean -i

它是互动的,你会得到一个快速的概述,什么将被删除,为您提供包含 / 排除受影响的文件的可能性。

你必須在 -d 如果你也想照顧空的文件夾。

git iclean

说到这一点,对互动命令的额外持有可能令人兴奋的经验丰富的用户,如今我只使用已经提到的 git clean -fd

其他回答

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

git clean -i

它是互动的,你会得到一个快速的概述,什么将被删除,为您提供包含 / 排除受影响的文件的可能性。

你必須在 -d 如果你也想照顧空的文件夾。

git iclean

说到这一点,对互动命令的额外持有可能令人兴奋的经验丰富的用户,如今我只使用已经提到的 git clean -fd

git clean -f -d -x $(git rev-parse --show-cdup)适用于干净的根目录,无论你在哪里呼叫它在仓库目录树内。

确保 -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。

注意: 首先,导航到目录并检查您要清理的分支。

-i 互动模式,它会告诉你什么将被删除,你可以从列表中选择一个操作。

清理文件只有 [文件不会列出并不会清理]: $ git clean -i 清理文件和文件夹: $ git clean -d -i

D 包括目录。


如果您从列表中选择 c 将删除未跟踪的文件/文件夹,并将删除您删除的文件/文件夹。

例如: 如果您在远程中重组文件夹并将更改拖到本地计算机上,则由其他人创建的文件/文件夹将最初在过去的文件夹和您重组的新文件夹中。

如果没有其他工作,可以使用下列组合来简单地删除“吉特状态”命令列出的所有变化:

git add -A& git commit -m temp && git reset --hard HEAD^

这将首先完成您的所有变化,然后创建一个暂时的承诺,然后放弃它。

我已经失败了使用最受欢迎的答案在这里 - git 不会从存储库中删除未追踪的文件,无论如何. 不知道为什么. 这是我的超简单的答案,没有特殊的 GIT 命令!

任务:从 git 存储库中删除未追踪的文件:

移动文件和文件夹到其他地方从您的本地项目文件夹一段时间 删除所有行在.gitignore 关于这些文件和文件夹的承诺 Git 添加. Git 承诺 -m “从未追踪的文件中清除存储库” Git push

所有文件和文件已从存储库中删除。

如果您需要,请在本地恢复它们:

将您暂时移动到本地项目文件夹的所有文件和文件夹返回 将这些文件和文件夹的所有行返回.gitignore Git 添加. Git commit -m “检查或文件不再显示在 git 存储库” Git push

你已经完成了!