If you do not wish to store certain files or files types in your Git
repository, ignore them by adding the file name patterns to the .gitignore
file. You may add multiple .gitignore
files in a repository. One at the root directory of the repository and one or more at subdirectories to apply distinct patterns specific to those subdirectories. Once you check-in .gitignore
file, all the developers working on that repository will get the same effect. This is standard practice.
However, if you work on multiple repositories and you want to apply similar ignore rules to multiple repositories, there are two options:
- Copy the
.gitignore
file to all the repositories - Use a global
.gitignore
file
In this article, I’ll show you the steps to use a global .gitignore
file to apply the exclusion pattern to all your repositories. Create a new file named .gitignore-global
(or any other name you like) at the user’s home directory or any other directory of your choice. Add all required exclusions to this file just like a normal.gitignore
file.
# Create a new file named .gitignore-global vi ~/.gitignore-global
The below exclusion patterns are just a sample.
# Package Files *.jar *.war *.nar *.ear *.zip *.tar.gz *.rar.settings .project .classpath # Eclipse Files .settings .project .classpath # Mac Files .DS_Store .DS_Store?
Now, change the Git
configuration to apply the above-created exclusions globally.
# Apply global gitignore by supplying the path to the global gitognore file git config --global core.excludesfile ~/.gitignore-global
When the .gitignore
file is checked into the Git repository, all collaborators get the exclusion list automatically when they clone the repository or pull the latest code. In case you use a global ignore list, every collaborator has to configure the global ignore list individually. This is a downside. I recommend continuing with the conventional way of having a .gitignore
file in the repository and also having a global ignore list to avoid the hassle of defining the same ignore list in every repository you work on.
Join our list to get instant access to new articles and weekly newsletter.
[…] .gitconfig […]