Custom git ignores with a global gitignore file or git exclude

If you use
git version control, you have probably seen
.gitignore
files in your projects. Its a
file that determines what doesn’t get included
in version control (see docs). These are commonly something like
node_modules/
or
venv/
where you install third party
packages or .env
for your environment
values.
But what if you want to have some files that are specific to you that you want
to ignore in your projects but that others don’t have, so configuring them in
a shared .gitignore
doesn’t make that
much sense?
I might have my editor settings in a
.vscode
folder or maybe a special
juhis-notes.md
file for personal notes.
I strongly believe you should prefix or postfix your personal files with your
name, that way they are way less likely to cause a problem later.
There are a couple of ways to ignore them and good people shared a bunch of tips recently in Mastodon.
Two main options are a global ignore file and using git exclude. Marijke Luttekes has a brilliant post that inspired this one where she goes more in-depth into the differences and when to use which. You should check that out as I won’t go that deep into the matter in this post, I just want to share good starting points for learning more.
Sebastian De Deyne has a blog post on
how to set up global ignore file.
To use a global ignore, you first create the file somewhere in your computer.
Sebastian has his in ~/.gitignore
, I
store mine in
~/.config/git/.gitignore
as that is
where
my dotfiles setup
links it to. It can be anywhere really.
You then tell git where to find it with
git config --global core.excludesfile ~/.gitignore
where the last argument is the path to your file.
Git exclude is another way to approach the problem. If you create a file at
.git/info/exclude
, you can define your
ignores in that file. The file is local so it’s only for you on that machine
and won’t be tracked by git.
Josh Thomas has written about
how he uses direnv to sync his own gitignore file into git exclude
because he “needs personal ignore files just infrequently enough that I can
never remember the exact path buried in
.git/
" I can feel that pain: it takes
quite a lot of repetition to get these tools into your muscle memory.
Git Scripts also has a handy guide for learning how git exclude works.
If something above resonated with you, let's start a discussion about it! Email me at juhamattisantala at gmail dot com and share your thoughts. In 2025, I want to have more deeper discussions with people from around the world and I'd love if you'd be part of that.