Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`git clean` removes ignored files by default?

Tags:

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?

like image 916
basin Avatar asked Apr 18 '14 06:04

basin


People also ask

Does git clean remove ignored files?

The git clean command also allows removing ignored files and directories.

Does git clean respect Gitignore?

Git normally doesn't clean ignored files unless the -x flag is specified, but strangely it cleans out when configured as you did ( folder/* ). As @VonC pointed out, you should change your . gitignore -file to ignore the directory ( data/ ) rather than its contents ( data/* ).

Does git clean delete files?

git clean deletes ignored files only if you use either the -x or -X option, otherwise it just deletes untracked files.


1 Answers

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.

like image 83
mvp Avatar answered Oct 04 '22 14:10

mvp