Ignoring files in a Git repo without touching .gitignore

Authored by

Over several years now I’ve used Git in developing patches for WordPress core, for which we committers still use SVN to actually commit patches, for better or worse. While working on patches in a fork, however, Git is definitely for better. I’ve found a lot of value in using GitHub for reviewing and collaborating on patches via pull requests. I actually have the SVN checkout and the Git clone both sitting in the same directory—.svn and .git sitting in a tree (👩‍❤️‍💋‍👨). There is a .gitignore committed into the WordPress repo even though it is is SVN, but it only ignores the most common files that may end up in the directory. Over the years I’ve accumulated a bunch of files in the directory tree (ones which I really should really do some spring cleaning on, but… 🍂). If I added those files to the .gitignore then it would also then appear as dirty unless I also then do git update-index --assume-unchanged .gitignore, but that seems liable to cause problems down the road. Today, however, I ran across an alternative in a feature of Git I hadn’t seen before.

From the git ready article on what’s inside your .git directory (from 2009):

There’s plenty of directories as well: […] info: Relatively uninteresting except for the exclude file that lives inside of it. We’ve seen this before in the ignoring files article, but as a reminder, you can use this file to ignore files for this project, but beware! It’s not versioned like a .gitignore file would be.

This is exactly what I’ve needed. I can add all of my untracked files in the directory tree to this file and not get noise anymore when I do git status. As a bonus, here’s a one-liner command to add all of the untracked files to be ignored:

git status --untracked-files=normal --porcelain
    | ack '^\?\?' 
    | cut -c4- 
    >> .git/info/exclude

Ah… “nothing to commit, working tree clean.”

3 thoughts on “Ignoring files in a Git repo without touching .gitignore”

  1. @weston that is pretty awesome. Aside from the handy tip re `.git/info/exclude` I actually never used the `cut` shell command. That is pretty neat too! Thanks for sharing that one liner.

  2. @weston excludes are one of the least known/used features of Git (IMO). You can also globally ignore specific files and patterns, but that can have unexpected consequences.

Leave a Reply to Rheinard Korf Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.