Skip to content
Tech News
← Back to articles

.gitignore Isn't the Only Way to Ignore Files in Git

read original more articles
Why This Matters

This article highlights that Git offers multiple ways to ignore files beyond the commonly used .gitignore, including per-repository and global ignore files. Understanding these options allows developers to better manage untracked files and streamline their workflows, improving efficiency and reducing clutter in version control. This knowledge is especially valuable for teams and individual developers aiming for cleaner repositories and personalized Git configurations.

Key Takeaways

I’ve been using Git for so long and I just realized you can ignore files at three different levels and not just with .gitignore . The three files you can use to ignore files are:

.gitignore

.git/info/exclude

~/.config/git/ignore

.gitignore is the usual file where you write files you want to ignore. It’s checked into Git along with the rest of the code. Whatever files you add to it will not get taken into account when running git commands.

The exclude file lives in the .git directory of every Git repository but changes to it are not checked into Git. It usually has a few comment lines on a fresh Git repository. This file is useful for ignoring things on a per-repo basis. For example, you may have a personal notes.txt file in a repository that you don’t want to check into git but you also don’t want to add to .gitignore because it’s unique to your workflow. In that case you would add notes.txt to .git/info/exclude .

The ignore file lives in your machine’s home directory in ~/.config/git/ignore . Whatever filenames are added to this file are ignored globally at a machine-level. This file is not checked into Git and isn’t associated with any particular repository. It’s a great place to add files that you want to ignore in every git repository on your computer. For example, if you’re on macOS, adding .DS_Store here would be ideal.

You can customize the global ignore file to be a different file. For example, if you want your global git ignore file to be .gitignore_global you would run the command:

Shell git config --global core.excludesFile ~/.gitignore_global

And if you ever want to return to the default setting, run:

... continue reading