问题
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 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.
回答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.
回答3:
Changing data/*
to data/
is not usable for me, because after that you can't whitelist files/folders in excluded folders.
when you put this in .gitignore
data/
!data/foo.txt
the file foo.txt
won't be included.
To remove all untracked files (and folders) as they are shown in git status
(and keep something like data/*
in gitignore) you can use
git ls-files -z -o --exclude-standard | xargs -0 rm -rf
This will list all untracked files and pass them to rm -rf
function, which will delete them.
Credits to https://stackoverflow.com/a/3801554/4710968
来源:https://stackoverflow.com/questions/19442616/how-to-preserve-all-ignored-files-in-git-clean-fd