Journal Archives About Books Talks

Local exclusions in Git

Suppose you have files you want to keep in your local folder without adding them to the Git index. Normally you'd add a rule to the project's .gitignore. If you want to avoid adding per-user cruft (and a Git commit) you can add exclusion rules to your global excludes file. This is a handy approach to ignore any per-project editor config and tmp files across all of your Git projects, but it's not a great fit for the one-off file in a single project.

Git provides another way to keep files out of the index via rules in .git/info/exclude. These rules are only applied locally and not shared with other clones.

I've run into enough scenarios to find this approach useful, but not often enough to remember the details, so I added the subcommand Git forgot, saving me several trips to Stack Overflow.

A custom git exclude

This little script accepts a pattern, creates the exclude file if necessary, and saves the pattern to the file:


mkdir -p .git/info
for pattern in "$@"
    echo "$pattern" >> .git/info/exclude

Now I can use git exclude <pattern> to ignore any path locally.

❯ touch

❯ git status --short

❯ git exclude

❯ git status --short

# Posted on