Last updated on 2023-09-18 | Edit this page
Estimated time 5 minutes
- How can I tell Git to ignore files I don’t want to track?
- 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 results $ touch a.csv b.csv c.csv results/a.out results/b.out
and see what Git says:
$ git status
On branch main Untracked files: (use "git add <file>..." to include in what will be committed) a.csv b.csv c.csv results/ 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
$ nano .gitignore $ cat .gitignore
These patterns tell Git to ignore any file whose name ends in
.csv and everything in the
(If any of these files were already being tracked, Git would continue to
Once we have created this file, the output of
is much cleaner:
$ git status
On branch main 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
.gitignore file. 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 data files and the results folder" $ git status
On branch main nothing to commit, working tree clean
As a bonus, using
.gitignore helps us avoid accidentally
adding files to the repository that we don’t want to track:
$ git add a.csv
The following paths are ignored by one of your .gitignore files: a.csv 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.csv. We can also always see the status of
ignored files if we want:
$ git status --ignored
On branch main Ignored files: (use "git add -f <file>..." to include in what will be committed) a.csv b.csv c.csv results/ nothing to commit, working tree clean
If you only want to ignore the contents of
results/plots, you can change your
to ignore only the
/plots/ subfolder by adding the
following line to your .gitignore:
This line will ensure only the contents of
is 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.
You would add the following two lines to your .gitignore:
*.csv # ignore all data files !final.csv # except final.csv
The exclamation point operator will include a previously excluded entry.
Note also that because you’ve previously committed
files in this lesson they will not be ignored with this new rule. Only
future additions of
.csv files added to the root directory
will be ignored.
Given a directory structure that looks similar to the earlier Nested Files exercise, but with a slightly different directory structure:
results/data results/images results/plots results/analysis
How would you ignore all of the contents in the results folder, but
Hint: think a bit about how you created an exception with the
! operator before.
If you want to ignore the contents of
results/ but not
results/data/, you can change your
.gitignore to ignore the contents of results folder, but
create an exception for the contents of the
subfolder. Your .gitignore would look like this:
results/* # ignore everything in results folder !results/data/ # do not ignore results/data/ contents
Assuming you have an empty .gitignore file, and given a directory structure that looks like:
results/data/position/gps/a.csv results/data/position/gps/b.csv results/data/position/gps/c.csv results/data/position/gps/info.txt results/plots
What’s the shortest
.gitignore rule you could write to
.csv files in
result/data/position/gps? Do not ignore the
results/data/position/gps/*.csv will match
every file in
results/data/position/gps that ends with
.csv. The file
results/data/position/gps/info.txt will not be ignored.
Let us assume you have many
.csv files in different
subdirectories of your repository. For example, you might have:
results/a.csv data/experiment_1/b.csv data/experiment_2/c.csv data/experiment_2/variation_1/d.csv
How do you ignore all the
.csv files, without explicitly
listing the names of the corresponding folders?
.gitignore file, write:
This will ignore all the
.csv files, regardless of their
position in the directory tree. You can still include some specific
exception with the exclamation point operator.
! modifier will negate an entry from a previously
defined ignore pattern. Because the
!*.csv entry negates
all of the previous
.csv files in the
.gitignore, none of them will be ignored, and all
.csv files will be tracked.
You wrote a script that creates many intermediate log-files of the
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