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

断了今生、忘了曾经 提交于 2019-11-29 04:40:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!