coding tips

Ignore files in git without editing .gitignore

You work on a repository where the .gitignore file is defined globally and already tracked by git, but you have some files in your local directory that you don’t want to be tracked (logs, or build files, *.pyc etc.) and would like to ignore them (because who doesn’t like git status to show “working tree clean” or the status bar in zsh to be green).

The problem

You can’t add them to the .gitignore file because that will cause git to report that there are changes to it and yet you don’t want to commit them to the repository (maybe different commiters use different build solutions and that’s why those files are not ignored globally)

The solution

The trick is to edit the file .git/info/exclude. It’s available locally per cloned repository. The syntax is the same as for the .gitignore file.

Example content.

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~



This way git will ignore your files, and you will not create an unnecessary commit, just because you use “this” setup, instead of “that” setup.