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

后端 未结 3 1911
小鲜肉
小鲜肉 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:16

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 02:34

    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.

    0 讨论(0)
提交回复
热议问题