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


当前回答

git clean -f

将会从当前 git 中删除未跟踪的文件

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.

我刚刚发明和尝试的这种情形的救生衣(非常有效):

git add .
git reset --hard HEAD

当心!确定承诺任何所需的更改(甚至在未跟踪的文件中)执行前先执行.

我感到惊讶的是,以前没有人提到这一点:

git clean -i

这代表着交互式互动您将快速了解要删除的内容, 以便您能够包含/ 排除受影响的文件。 总的来说, 仍然比执行强制任务要快--dry-run在真正的清洁之前。

你将不得不扔在-d如果您也想要处理空文件夹。最后,它会制作一个漂亮的别名:

git iclean

话虽如此,对有经验的用户来说,持有互动命令的手掌外握,可能会使有经验的用户感到疲劳。git clean -fd

我在这里没有使用最流行的答案- git 不删除存储处的未追踪文件 。 不知道为什么 。 这是超简化的答案, 没有特别的 GIT 指令 !

任务: 从 git 仓库删除未跟踪的文件 :

  1. 将本地工程文件夹中的文件和文件夹移动一段时间
  2. 删除 . gitignore 中关于此承诺的文件和文件夹的所有行
  3. Git 添加 。
  4. Git 承诺 -m “从未跟踪的文件中清除存储库”
  5. Git 推动

从仓库中删除了所有文件和文件夹 。

如果您需要的话, 请在本地主机上恢复它们 :

  1. 将所有已临时移至本地工程文件夹的文件和文件夹再移到本地工程文件夹
  2. 将这些文件和文件夹的所有行后移到. gitignore
  3. Git 添加 。
  4. Git 承诺 -m “检查或在 Git 仓库中不再出现文件”
  5. Git 推动

你完蛋了! 你完蛋了! You are done!

我们可以使用下面的 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