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

后端 未结 3 1913
小鲜肉
小鲜肉 2020-12-29 01:46

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 un

3条回答
  •  粉色の甜心
    2020-12-29 02:28

    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

提交回复
热议问题