How do I configure git to ignore some files locally?

前端 未结 11 1196
深忆病人
深忆病人 2020-11-22 01:03

Can I ignore files locally without polluting the global git config for everyone else? I have untracked files that are spam in my git status but I don\'t want to commit git c

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 01:34

    You can install some git aliases to make this process simpler. This edits the [alias] node of your .gitconfig file.

    git config --global alias.ignore 'update-index --skip-worktree'
    git config --global alias.unignore 'update-index --no-skip-worktree'
    git config --global alias.ignored '!git ls-files -v | grep "^S"'
    

    The shortcuts this installs for you are as follows:

    • git ignore config.xml
      • git will pretend that it doesn't see any changes upon config.xml — preventing you from accidentally committing those changes.
    • git unignore config.xml
      • git will resume acknowledging your changes to config.xml — allowing you again to commit those changes.
    • git ignored
      • git will list all the files which you are "ignoring" in the manner described above.

    I built these by referring to phatmann's answer — which presents an --assume-unchanged version of the same.

    The version I present uses --skip-worktree for ignoring local changes. See Borealid's answer for a full explanation of the difference, but essentially --skip-worktree's purpose is for developers to change files without the risk of committing their changes.

    The git ignored command presented here uses git ls-files -v, and filters the list to show just those entries beginning with the S tag. The S tag denotes a file whose status is "skip worktree". For a full list of the file statuses shown by git ls-files: see the documentation for the -t option on git ls-files.

提交回复
热议问题