Contents

Git While You Sit 2 - .gitignore

Contents

This is part of the “Git While You Sit” series, a play on Google’s Testing on the Toilet. It’s intended to fit on a printed page. Currently Chrome doesn’t seem to correctly print columns, but Firefox does. {: .no-print }

Your repository has files which are generated as part of your build process or as part of running your software, which you don’t want in source control. They keep showing up in git status. What to do?

You can create a file called .gitignore - note that the filename starts with a ., which is standard for configuration files in Unix and causes them to be hidden from normal listing. Each .gitignore file affects the current directory and its subdirectories - you can have multiple .gitignore files to create more specific rules for subdirectories.

Note: .gitignore can only be used for files which shouldn’t be in source code at all (those show up as “Untracked files”. Modified files can’t be ignored in this way. If you really want to, you can force git to ignore modifications with this command:

1
git update-index --assume-unchanged FILE

However, this is usually a bad idea and indicates you need to refactor your file handling - split files which get modified locally from files which contain information which should be source-controlled.

Here is an annotated excerpt from a .gitignore file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Extensions of compiled files
*.a
*.so
*.o
# ...

# Files generated by build system
build.ninja
.ninja_deps

# Ignore bin/ and obj/, as they contain
# compiled files. This is ignored
# recursively within the repository.
bin/
obj/

# ...except ("!") for the scripts, which
# are in the "scripts" dir in the same
# one as this .gitignore file (hence the
# leading "/")
!/scripts/bin

Addendum: A reader has mentioned gitignore.io, which auto-generates useful .gitignore files.