-->

How to preserve all ignored files in git clean -fd

2019-02-04 00:31发布

问题:

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?

回答1:

Git normally don't clean ignored files unless you specify the -x flag, but strangely it cleans out when configured as you did (folder/*).

As @VonC commented it out, you should change your .gitignore to ignore the directory (data/) rather than what's in it (data/*).

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



回答2:

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.



标签: git git-clean