How do I configure git to ignore some files locally?

前端 未结 11 888
深忆病人
深忆病人 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:31

    From the relevant Git documentation:

    Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user's workflow) should go into the $GIT_DIR/info/exclude file.

    The .git/info/exclude file has the same format as any .gitignore file. Another option is to set core.excludesFile to the name of a file containing global patterns.

    Note, if you already have unstaged changes you must run the following after editing your ignore-patterns:

    git update-index --assume-unchanged <file-list>
    

    Note on $GIT_DIR: This is a notation used all over the git manual simply to indicate the path to the git repository. If the environment variable is set, then it will override the location of whichever repo you're in, which probably isn't what you want.


    Edit: Another way is to use:

    git update-index --skip-worktree <file-list>
    

    Reverse it by:

    git update-index --no-skip-worktree <file-list>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 01:39

    Update: Consider using git update-index --skip-worktree [<file>...] instead, thanks @danShumway! See Borealid's explanation on the difference of the two options.


    Old answer:

    If you need to ignore local changes to tracked files (we have that with local modifications to config files), use git update-index --assume-unchanged [<file>...].

    0 讨论(0)
  • 2020-11-22 01:39

    You can simply add a .gitignore file to your home directory, i.e. $HOME/.gitignore or ~/.gitignore. Then tell git to use that file with the command:

    git config --global core.excludesfile ~/.gitignore
    

    This is a normal .gitignore file which git references when deciding what to ignore. Since it's in your home directory, it applies only to you and doesn't pollute any project .gitignore files.

    I've been using this approach for years with great results.

    0 讨论(0)
  • 2020-11-22 01:41

    this is a kind of brief solution,that add a row to local exclude file.

     echo YOURFILE_OR_DIRECTOY >> .git/info/exclude 
    
    0 讨论(0)
  • 2020-11-22 01:43

    I think you are looking for:

    git update-index --skip-worktree FILENAME
    

    which ignore changes made local

    Here's http://devblog.avdi.org/2011/05/20/keep-local-modifications-in-git-tracked-files/ more explanation about these solution!

    to undo use:

    git update-index --no-skip-worktree FILENAME
    
    0 讨论(0)
提交回复
热议问题