OverviewTeaching: 5 min
Exercises: 0 minQuestions
How can I tell Git to ignore files I don’t want to track?Objectives
Configure Git to ignore specific files.
Explain why ignoring files can be useful.
What if we have files that we do not want Git to track for us, like backup files created by our editor or intermediate files created during data analysis? Let’s create a few dummy files:
$ mkdir receipts $ touch a.png b.png c.png receipts/a.jpg receipts/b.jpg
and see what Git says:
$ git status
On branch master Untracked files: (use "git add <file>..." to include in what will be committed) a.png b.png c.png receipts/ nothing added to commit but untracked files present (use "git add" to track)
Putting these files under version control would be a waste of disk space. What’s worse, having them all listed could distract us from changes that actually matter, so let’s tell Git to ignore them.
We do this by creating a file in the root directory of our project called
$ nano .gitignore $ cat .gitignore
These patterns tell Git to ignore any file whose name ends in
and everything in the
(If any of these files were already being tracked,
Git would continue to track them.)
Once we have created this file,
the output of
git status is much cleaner:
$ git status
On branch master Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore nothing added to commit but untracked files present (use "git add" to track)
The only thing Git notices now is the newly-created
You might think we wouldn’t want to track it,
but everyone we’re sharing our repository with will probably want to ignore
the same things that we’re ignoring.
Let’s add and commit
$ git add .gitignore $ git commit -m "Ignore png files and the receipts folder." $ git status
On branch master nothing to commit, working directory clean
As a bonus, using
.gitignore helps us avoid accidentally adding to the repository files that we don’t want to track:
$ git add a.png
The following paths are ignored by one of your .gitignore files: a.png Use -f if you really want to add them.
If we really want to override our ignore settings,
we can use
git add -f to force Git to add something. For example,
git add -f a.dat.
We can also always see the status of ignored files if we want:
$ git status --ignored
On branch master Ignored files: (use "git add -f <file>..." to include in what will be committed) a.png b.png c.png receipts/ nothing to commit, working directory clean
Ignoring Nested Files
Given a directory structure that looks like:
How would you ignore only
If you only want to ignore the contents of
receipts/plots, you can change your
.gitignoreto ignore only the
/plots/subfolder by adding the following line to your .gitignore:
This line will ensure only the contents of
receipts/plotsis ignored, and not the contents of
As with most programming issues, there are a few alternative ways that one may ensure this ignore rule is followed. The “Ignoring Nested Files: Variation” exercise has a slightly different directory structure that presents an alternative solution. Further, the discussion page has more detail on ignore rules.
Including Specific Files
How would you ignore all
.pngfiles in your root directory except for
final.png? Hint: Find out what
!(the exclamation point operator) does
You would add the following two lines to your .gitignore:
*.png # ignore all png files !final.png # except final.png
The exclamation point operator will include a previously excluded entry.
Note also that because you’ve previously committed
.pngfiles in this lesson they will not be ignored with this new rule. Only future additions of
.pngfiles added to the root directory will be ignored.
Ignoring Nested Files: Variation
Given a directory structure that looks similar to the earlier Nested Files exercise, but with a slightly different directory structure:
receipts/data receipts/images receipts/plots receipts/analysis
How would you ignore all of the contents in the receipts folder, but not
Hint: think a bit about how you created an exception with the
If you want to ignore the contents of
receipts/but not those of
receipts/data/, you can change your
.gitignoreto ignore the contents of receipts folder, but create an exception for the contents of the
receipts/datasubfolder. Your .gitignore would look like this:
receipts/* # ignore everything in receipts folder !receipts/data/ # do not ignore receipts/data/ contents
Ignoring all data Files in a Directory
Assuming you have an empty .gitignore file, and given a directory structure that looks like:
receipts/data/market_position/gps/a.dat receipts/data/market_position/gps/b.dat receipts/data/market_position/gps/c.dat receipts/data/market_position/gps/info.txt receipts/plots
What’s the shortest
.gitignorerule you could write to ignore all
result/data/market_position/gps? Do not ignore the
receipts/data/market_position/gps/*.datwill match every file in
receipts/data/market_position/gpsthat ends with
.dat. The file
receipts/data/market_position/gps/info.txtwill not be ignored.
The Order of Rules
.gitignorefile with the following contents:
What will be the result?
!modifier will negate an entry from a previously defined ignore pattern. Because the
!*.datentry negates all of the previous
.datfiles in the
.gitignore, none of them will be ignored, and all
.datfiles will be tracked.
You wrote a script that creates many intermediate log-files of the form
log_03, etc. You want to keep them but you do not want to track them through
.gitignoreentry that excludes files of the form
Test your “ignore pattern” by creating some dummy files of the form
You find that the file
log_01is very important after all, add it to the tracked files without changing the
Discuss with your neighbor what other types of files could reside in your directory that you do not want to track and thus would exclude via
- append either
log*as a new entry in your .gitignore
git add -f log_01
.gitignorefile tells Git what files to ignore.