Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve all ignored files in git clean -fd?

Tags:

git

git-clean

When I have .gitignore data/* and run git clean -fd, the data folder and all its content files are deleted.

What I want is to delete all unrevisioned files in a git repo while excluding all ignored files (i.e. DON'T delete gitignored files). What could I do?

like image 797
Danny Lin Avatar asked Oct 18 '13 05:10

Danny Lin


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/* ).

How do I save git ignore?

Use your favorite text editor to open the file called . git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.


2 Answers

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/*).

It's a subtle difference, but it matters to git.

like image 88
talles Avatar answered Oct 22 '22 05:10

talles


I've found some more details. Having /tmp/* in gitignore, git clean -fd will remove it. As it was said in other answers, this does not happens with /tmp/ in gitignore.

But once you have any checked-in any file in this directory, git clean -fd will ignore this path. This can be achieved with git add -f or adding !/tmp/.keep to gitignore and checking this file in.

like image 1
prcu Avatar answered Oct 22 '22 07:10

prcu