`git clean`默认删除被忽略的文件? [英] `git clean` removes ignored files by default?

查看:307
本文介绍了`git clean`默认删除被忽略的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据帮助,没有 -x 选项 git clean 应该放弃被忽略的文件,但它不会't。

  [il @ reallin test] $ cat .gitignore 
* .sar
[il @reallin测试] $ mkdir -p conf / sar&& touch conf / sar / aaa.sar
[il @ reallin test] $ git status
#在分支大师
上没有提交,工作目录干净
[il @ reallin test] $ git clean -df
删除conf /

conf / sar /aaa.sar 被删除。根据 man git clean / p>

  -d 
除了未跟踪的文件以外,还要除去未跟踪的目录。

在你的情况下,目录 conf / sar 没有被跟踪 - 它不包含任何被git跟踪的文件。如果你没有gitignore规则并且执行了 git clean -fd ,那么这个未被跟踪的目录的内容就会被删除 - 正如文档说的那样。



现在,如果在规则中添加 .gitignore 来忽略 *。sar 文件,它并没有改变你的目录 conf / sar / 仍然未被跟踪,并且有符合这个gitignore规则的未跟踪文件aaa.sar的基本事实不应该突然使它不能被 git clean -fd



但是,如果您在忽略的 aaa.sar ,那么这个目录不会被删除,你的文件将被保留。



换句话说,虽然它看起来很混乱,这不是一个bug,git完全是文档说的。


According to the help, without -x option git clean should let alone the ignored files, but it doesn't.

[il@reallin test]$ cat .gitignore
*.sar
[il@reallin test]$ mkdir -p conf/sar && touch conf/sar/aaa.sar
[il@reallin test]$ git status
# On branch master
nothing to commit, working directory clean
[il@reallin test]$ git clean -df
Removing conf/

conf/sar/aaa.sar is removed. Is it a bug?

解决方案

According to man git clean:

-d
    Remove untracked directories in addition to untracked files.

In your case, directory conf/sar is not tracked - it does not contain any files that are tracked by git. If you did not have gitignore rule and executed git clean -fd, contents of this untracked directory would have been removed - just what documentation says.

Now, if you add .gitignore with rule to ignore *.sar files, it does not change basic fact that your directory conf/sar/ is still untracked, and having untracked file aaa.sar that is eligible for this gitignore rule should not suddenly make it unremovable by git clean -fd.

But, if you add any tracking file next to your ignored aaa.sar, then this directory would not be removed and your file will be left alone.

In other words, while it looks confusing, this is not a bug and git does exactly what documentation says.

这篇关于`git clean`默认删除被忽略的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆