Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

`git clean` removes ignored files by default?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...