236

I am a bit confused about the pros and cons of using .git/info/exclude and .gitignore to exclude files.

Both of them are at the level of the repository/project, so how do they differ and when should we use .git/info/exclude ?

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Parag
  • 12,093
  • 16
  • 57
  • 75

4 Answers4

305

The first advantage of .gitignore is that it is versioned into the repository itself, unlike .git/info/exclude. The second advantage is that you can have multiple .gitignore files, one per directory/subdirectory, for directory specific ignore rules, unlike .git/info/exclude.

So the .gitignore files are versioned and present across all clones of the repository. Therefore, in large teams all people are ignoring the same kind of files (e.g. *.db, *.log); and using several .gitignore files allow for more specific ignore rules.

.git/info/exclude is available for individual clones only. It is not versioned, hence what one person ignores in their clone is not available/present in another person's clone. For example, if someone uses Eclipse for development, it may make sense for that developer to add .build folder to .git/info/exclude because other devs may not be using Eclipse.

In general, files/ignore rules that have to be universally ignored should go in .gitignore, and otherwise files that you want to ignore only on your local clone should go into .git/info/exclude.

MacroController
  • 322
  • 2
  • 6
  • 19
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • but that should ideally go into ~/.gitignore as per Git official documentation here, https://git-scm.com/docs/gitignore – Deven Jan 10 '20 at 00:18
  • @DevendraSwami I didn't get which specific entry should go into `~/.gitignore` in your comment above. My understanding is that ignore rules can be at 3 levels - `$PROJECT/.git/info/exclude` for (project, user) specific ignore rules, `$PROJECT//.gitignore` which is for project specific ignore rules across any user anywhere (when checked in), `~/.gitignore` for user specific ignore rules for any project for that user on that machine. Based on the objective, you pick the place to put an entry in. – Anshul Goyal Jan 10 '20 at 11:51
  • yeah, you are absolutely right. My comment was related to this question https://stackoverflow.com/questions/59673991/when-would-you-use-git-info-exclude-instead-of-gitignore-core-excludesfile – Deven Jan 10 '20 at 23:03
  • I'm trying to add the sln file itself to the exclude file. Everything I tried doesn't work and solution keeps being recognized as changed. – Shimmy Weitzhandler Apr 28 '20 at 11:07
  • 4
    @ShimmyWeitzhandler, Is the sln file already in your repo? Then exclude or .ignore will not prevent git from tracking it's changes. Try one of the following: `git rm --cached ` will delete it from the repository, but keep it locally. `git update-index --skip-worktree ` will ignore changes to the file, but keep it in the repository. Out of curiosity: Why do you want the sln-file excluded? It's an important part of a .Net solution right? – Koen May 19 '20 at 09:29
74

Googled : 3 ways of excluding files

  1. .gitignore applies to every clone of this repository (versioned, everyone will have it),
  2. .git/info/exclude only applies to your local copy of this repository (local, not shared with others),
  3. ~/.gitignore applies to all the repositories on your computer (local, not shared with others).

3. actually requires to set a configuration on your computer :

git config --global core.excludesfile '~/.gitignore'
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    The linked blog mistakenly calls the file `.git/info/excludes`, when it should be `.git/info/exclude`, as confirmed by the documentation it links to. – mwfearnley Nov 16 '15 at 12:31
  • 17
    (spoiler: the third way is computer-global ignore through a setting in ~/.gitconfig) – hmijail Mar 21 '16 at 18:58
  • While we're on the topic of excluding files, here's one more: `git update-index --assume-unchanged ...`. It works on files already in the repo. – TWiStErRob May 08 '21 at 10:56
24

Just to offer our (real world) experience: we started using .git/info/exclude when we had to customize some config files on each development environment but still wanted the source to be maintained in the repo and available to other developers.

This way, the local files, once cloned and modified can be excluded from commits without affecting the original files in the repo but without necessarily being ignored in the repo either.

Effing
  • 341
  • 2
  • 6
10

Use .gitignore for ignore rules that are specific to the project. Use exclude or a global ignore file for ignore rules that are specific to your environment.

For example, my global ignore files ignore the temp files generated by whatever editor I’m using—that rule is specific to my environment, and might be different for some other developer on the same project (perhaps they use a different editor). OTOH, my project .gitignore files ignore things like API keys and build artifacts—those are for the project, and should be the same for everyone on the project.

Does that help?

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33