Git: Exclude a file with git clean

前端 未结 5 1023
灰色年华
灰色年华 2020-12-14 14:45

i\'m working on a big python project, and i\'m really sick if .pyc and *~ files. I\'d like to remove them. I\'ve seen that the -X flag of git clean would remove

5条回答
  •  有刺的猬
    2020-12-14 15:17

    I put local files that fall into this category in .git/info/exclude (e.g. my IDE project files). They you can do a clean like this:

    git ls-files --others --exclude-from=.git/info/exclude -z | \
        xargs -0 --no-run-if-empty rm --verbose
    

    Where:

    • --others: shows untracked files
    • --exclude-from: provide a standard git ignore style file to exclude from the list
    • -z / -0: use \0 instead of \n to split names
    • --no-run-if-empty: Don't run rm if list is empty

    You could create an alias, e.g.:

    git config --global alias.myclean '!git ls-files --others --exclude-from=.git/info/exclude -z | xargs -0 --no-run-if-empty rm --verbose --interactive'
    

    The --interactive means you have to do git myclean -f to force the deletion.

    Reference: http://git-scm.com/docs/git-ls-files (plus the first line of the default .git/info/exclude)

提交回复
热议问题