Recently, I have been involved in reviewing pull requests as part of different projects. Often, contributors tend to use an editor different than what is used by others in the same repository, this requires them to also update the .gitignore to ignore the editor configurations from commits.

Therein lies the problem, if everyone started to commit to their environment it is going to create a super long list within the .gitignore which will be harder to maintain as the development progresses. There should not be the need for the repository to maintain and track the different contributor’s editor configurations.

A better solution to this is to use the personalised global .gitignore that would be applicable to all the repositories a contributor is working on. Surprisingly, not many people are aware of the global .gitignore which is used for the solution to this issue.

Global .gitignore

Firstly create a .gitignore file that would encapsulate all the global rules applicable to git operations throughout the system. As a general rule of thumb, people tend to create this in their home directory. To do so, simply do the following

touch ~/.gitignore

Note: The name .gitignore and the path where the file is created does not matter, one can directly replace the path and name with a different one

Second, simply edit the newly created .gitignore with the text editor and add the files and folders that need to be always ignored. A basic and minimum global configuration in such file would be somewhat like the following:

.DS_Store
.vscovde
.idea

Generally, a .gitignore will have three entries, one is system-specific (.DS_Store for macOS), and two editor specific (.vscode for Visual Code and .idea for IntelliJ). There are others that should be included, for instance, Thumbs.db for Windows systems. Once you have the .gitignore ready, you can configure the file to be excluded during system-wide invocation of the git by setting it up just once:

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

On windows or Powershell, the command becomes:

git config --global core.excludesfile "%USERPROFILE%\.gitignore"
git config --global core.excludesFile "$Env:USERPROFILE\.gitignore"

A successful inclusion would add another entry in the .gitconfig of the user which would look something like:

[core]
    excludesfile = {path-to-home-dir}/.gitignore

A sample of a global .gitignore is appended here for reference. It is certainly not an exhaustive one but does it work :)

###################
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc
*.pyo

############
# Packages #
############
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.msi

######################
# Logs and databases #
######################
*.log
*.sql
*.sqlite

######################
# OS generated files #
######################
.DS_Store
._*
.Trashes
Thumbs.db
desktop.ini

###################
# Temporary files #
###################
*.bak
*.swp
*.swo
*~
*#

#############
# IDE files #
#############
.vscode
.idea
*.sublime-workspace