.gitignore is a file that tells Git which files to ignore.
Git sees files in your directory 3 ways:
Ignored files are files that should never be committed. For example, in AP we
never want to commit our object files or executables. We can indicate to Git
that we want these files to be ignored using our
We can create a
.gitignore with the following command:
Now let’s start by telling Git to ignore a specific file. For example,
let’s say we have an executable file,
a.out, that we want to ignore.
An easy way to do this is to append “a.out” to
echo a.out >> .gitignore
We can also open
.gitignore with a text editor such as Vim to add files
to be ignored, with each file on a separate line. We can use
# to comment
lines and can also add blank lines to make our file more readable.
Now Git will ignore any file in our repository named “a.out”. We can be more
specific about the location of the ignored file by using
/ to indicate the file
path relative to
.gitignore. For example, the line
will ignore a file
a.out in the same directory as
will ignore a file named “a.out” in the part1 directory.
We can use
! to create an exception to our rule, so that a file we have told
Git to previously ignore is no longer ignored.
For example, if
.gitignore contained the following:
Anything named “a.out” will be ignored by Git except a file
a.out in the part1
Files already tracked by Git will not be affected by changes to
If we want to stop tracking a file we can use:
git rm --cached FILENAME
This change needs to be committed for Git to stop tracking the file.
Each line of
.gitignore is called a pattern. Git matches filenames
in our repository with these patterns. So far, we have used exact filenames
as patterns to tell Git what files should be ignored. We can use globbing
to create more general patterns. Globbing is an operation that recognizes
wildcard characters in a pattern and expands them into pathnames that match
* is a wildcard and matches with any characters, zero or more, except
For example, we can use
*.o to ignore any files ending with
We can use
? to match with any one character except
/. If our directory
has many files called
test4.c, etc. We can
ignore all these files with the patten
We can also use square brackets
 to match one character to a set of characters
within the brackets. If we have files,
testC., that we
want to ignore, we can use the pattern
We can use range notation to make this more succinct. The pattern
will ignore the same files as
test[ABC].c. Both alphabetical and numeric
ranges can be used within square brackets. The pattern
match with both
test0.c but not
While not a perfect analogy,
** can be used with directories similarly
* is used with characters.
We can use
** between two
/ to match with zero or more directories.
/tmp/**/*.o will match with any filesending in
.o, within any directory
that is a descendant of
tmp, such as
/** will match with anything inside a directory.
/ignore/** will match
with all files in the
**/ matches all directories.
**/test* matches any file beginning with
“test” and is equivalent to
If you want to check why a file is being ignored you can use the following command to see what pattern the file matches with:
git check-ignore -v [filename]
This will output the
.gitignore file with the pattern, the line number
of the pattern, the pattern that is causing the file to be ignored,
and the ignored file.
This guide was created by Chloe Ho using the following resources:
Last updated: 2022-11-08