180

I want to change the gitignore, but not everyone on the team wants these changes. How can a user have their own specific git ignore file?

asdsadasds
  • 1,803
  • 2
  • 12
  • 4
  • A very concise answer is actually provided in the [documentation](https://git-scm.com/docs/gitignore#_description). – djvg Sep 27 '21 at 07:44

6 Answers6

173

You can create your own .gitignore using

git config --global core.excludesfile $HOME/.gitignore

Then put your desired entries in that file.

Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
Dave Kincaid
  • 3,970
  • 3
  • 24
  • 32
  • 34
    If you don't want it to be global, you can put your `.user_gitignore` file under repo's `.git` directory and add it with `git config core.excludesfile .git/.user_gitignore` – orad Jan 28 '15 at 20:36
  • 10
    [Pro-tip from Tim Pope](https://github.com/tpope/vim-pathogen#faq): use `~/.cvsignore` because utilities like rsync will use this file as well. – Peter Rincker Mar 06 '15 at 17:57
  • 5
    @orad I think your comment deserves to be its own separate answer. – Stevoisiak Nov 07 '17 at 15:34
  • 3
    Take in account that orad's/Dave's suggestion will overwrite the default of `core.excludesfile` pointing to your global ignore file ie: `~/.gitignore`. If you want to preserve global exclusions and have repository specific exclusions as well, @grzuy's answer is the way to go – sgimeno Mar 01 '18 at 10:32
  • 1
    On Git for Windows, `$HOME/.gitignore` did not work correctly for me, but `~/.gitignore` did. – Soren Bjornstad Oct 28 '19 at 18:12
  • @orad If you don't want it to be global, just edit `$your_repo_path/.git/info/exclude`, which applies to that repo. – 200_success Feb 04 '23 at 01:57
152

For user-specific and repo-specific file ignoring you should populate the following file:

$GIT_DIR/info/exclude

Usually $GIT_DIR stands for:

your_repo_path/.git/
grzuy
  • 4,791
  • 2
  • 20
  • 17
  • 11
    For those confused about the path: `your_repo/.git/info/exclude`. The file is formatted like a standard [.gitignore](https://git-scm.com/docs/gitignore) file. – Stevoisiak Nov 07 '17 at 15:37
  • 10
    If your file already contains unstaged changes you may need to run `git update-index --skip-worktree [...]` (from https://hashrocket.com/blog/posts/ignore-specific-file-changes-only-on-current-machine-in-git) – Daniel Olivares Jan 17 '18 at 01:55
43

In their .gitconfig:

[core]
    excludesfile = ~/.global_gitignore

That way, they can ignore certain types of files globally. Each user can have their own global ignore file.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
9

For example, you want ignore ~/some/path/.idea folder:

# 1. Add .idea to user specific gitignore file
echo .idea > ~/.gitignore

# 2. Add gitignore file to gitconfig
git config --global core.excludesfile ~/.gitignore
Sergey Zhigalov
  • 1,019
  • 8
  • 3
6

As indicated in Atlassian's .gitignore tutorial, you could also use your repo's <repo>/.git/info/exclude file that you can easily edit with any text editor. It works the same as .gitignore.

I could easily ignore my intelliJ files, personal dockerfiles and stuff only I need to work with.

NickD
  • 5,937
  • 1
  • 21
  • 38
obscure18
  • 169
  • 4
  • 8
  • Yes! Edit the `exclude` file to get *repo specific* ignore of files or file patterns. Great for scripts that I want to use for that repo and _store_ in that repo, but don't want committed to the project at large, and don't want cluttering the `.gitignore` file that is shared by everybody. – Purplejacket Feb 08 '21 at 23:51
  • Careful with this one. I've had two occasions now where git ends up split-brained about files in the `.git/info/exclude` folder and refuses to let me do most operations (because it thinks the files are dirty) and refuses to let me restore them (because it thinks the files are to be ignored). Even removing the files from `exclude` doesn't resolve the issue -- I had to discard the entire folder and re-clone the project from a remote. – Techrocket9 Feb 13 '23 at 22:01
1

Global (user) scope ignore

Per gitignore Documentation, a user-level excludesfile default exists without editing git config --global core.excludesfile:

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

So one only need create (and populate) the relevant file, usually $HOME/.config/git/ignore (and possibly path):

if [ -z "${XDG_CONFIG_HOME}" ] ;
then 
    echo "XDG_CONFIG_HOME unset or empty, try \$HOME" ;
    if [ -z "${HOME+x}" ]
    then
        echo "Variables for default not set"
        echo "use explicit \`git config --global core.excludesfile EXCLUDES_FILE\`"
    else
        echo using \$HOME
        mkdir -p "$HOME/.config/git" && touch "$HOME/.config/git/ignore"
    fi
else
    echo using \$XDG_CONFIG_HOME
    mkdir -p "$XDG_CONFIG_HOME/git" && touch "$XDG_CONFIG_HOME/git/ignore"

fi

Caveat: This configuration is implicit rather than explicit. Consider setting git config --global core.excludesfile to the chosen file, even if it is a default.

Repo scope user-specific ignore

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.

So from the root of the repo, edit .git/info/exclude

dmacduff
  • 51
  • 3