Quick tip: Ignore files in Git without adding them to .gitignore
Posted on
TL;DR: Use .git/info/exclude
to ignore files without adding them to
.gitignore
.
In collaborative development environments, you often need to ignore certain files in Git. Depending on your needs, there are three ways to achieve this:
Project .gitignore
The default way to ignore files in Git is to add them to the project-specific
.gitignore
file. This file is tracked in Git, so your other teammates will see
this change as well.
Global .gitignore
Sometimes, you need to ignore files that are specific to your personal setup. I
prefer to put these kinds of files into my global .gitignore
. This has two
benefits:
- The global
.gitignore
file applies to all projects which is great for files that my personal setup creates for all projects. - It is not shared with others. Because files related to your personal setup
are very individual, often it is not desirable to put them into the shared
.gitignore
file.
To use a global .gitignore
file, you need to configure its path using:
git config --global core.excludesFile <path-to-global-gitignore>
I personally like to put it at ~/.gitignore
.
Exclude file
My discovery of this last method is my main reason for this blog post. While
the project-specific and global .gitignore
files seem to be widely known, I
haven't seen the .git/info/exclude
file discussed publicly before.
The .git/info/exclude
file works similar to the .gitignore
file, however,
it is not checked into Git. Consequently, you can use it to ignore
project-related local files that don't apply to other people.
A common use-case for me is files and folders that I create during debugging,
such as logs or other context. These files often do not follow a common naming
scheme, so adding them to the shared .gitignore
would be confusing for others.
At the same time, these files are specific to the project, so adding them to the
global .gitignore
wouldn't make sense either.
Previously, I kept these kinds of files in my staging area and just never added them to a commit. However, I've recently migrated to the Jujutsu version control system which automatically tracks all files that are not ignored. Consequently, I started being stricter with my Git ignore files.
I hope you find this quick tip useful. Since this is a little bit different from my other posts, I'd love to hear your opinion on these smaller posts in the comments below.