【问题标题】:Telling git its ok to remove untracked files [duplicate]告诉 git 可以删除未跟踪的文件 [重复]
【发布时间】:2011-11-27 03:29:35
【问题描述】:

可能重复:
How do you remove untracked files from your git working copy?

是否可以告诉 git 删除未跟踪的文件?主要是类似于重置的东西?

示例:

git checkout -- index.php <-- revert my file
git checkout -- master <-- this would revert the entire repo back to the last commit on master, removing (deleting) any and all untracked files as well as reverting committed ones.

我知道这在 shell 上是微不足道的。但我想知道这是否可以在 Git 中完成?

【问题讨论】:

  • git checkout -- master不会签出分支 master,但会在当前提交时签出名为 master文件(@ 987654326@)

标签: git version-control


【解决方案1】:

您需要git clean,但添加-df 以启用删除您所在目录中的文件。添加x 以包含被忽略的文件。

所以要彻底清理您的工作目录,只留下源代码控制中的内容,请发出以下命令:

git clean -xdf

【讨论】:

  • 确实支持。 git-scm.com/docs/git-clean
  • 这是问题的最终答案。接受的答案会清除未跟踪目录的内部,但会保留目录本身。
【解决方案2】:

您可能正在寻找git clean。这将删除所有未跟踪的文件。默认情况下,这会忽略(不删除).gitignore 中的模式,但git clean -x 也会清理这些文件。

来自git clean 手册页:

   -x
       Don't use the ignore rules. 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.

【讨论】:

  • 我必须改用-f-x 似乎对我不起作用,出现此错误:fatal: clean.requireForce defaults to true and neither -i, -n nor -f given; refusing to clean
  • @Hlung:看起来你的 Git 安装默认需要 -f。所以你会使用git clean -f -x
猜你喜欢
  • 2016-02-27
  • 2018-07-21
  • 2017-05-10
  • 2017-10-23
  • 2017-11-30
  • 2012-04-02
  • 2013-11-17
  • 2014-02-12
  • 1970-01-01
相关资源
最近更新 更多