Ignoring Things
Overview
Teaching: 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.
There are times when we have files that we do not want Git to track in our repositories. Some examples of this might be:
- output files generated from scripts
- temporary files made by our favorite text editor
- sensitive information
- log files
In our cats-as-data
project, we have such a file. Let’s run git status
:
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
cats-human-situations.md
nothing added to commit but untracked files present (use "git add" to track)
The file we want to ignore is the cats-human-situations.md
file. This file has
been generated by a CSV to Markdown tool, which we found helpful to review
locally. But it is a derivative of the authoritative CSV file, and others may
not find it useful. So let’s tell Git to ignore it.
We do this by opening the .gitignore
file in our text editor and adding the name of
the file in a line below the comments:
# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile ~/.gitignore_global
cats-human-situations.md
Now that we have told Git we want to ignore this file, let’s check git status
again.
$ 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 .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 .gitignore
:
$ git add .gitignore
$ git commit -m "Add the ignore file"
$ git status
# On branch master
nothing to commit, working directory clean
There are other patterns you can use to have Git ignore entire directories, or
groups of files. For now, what’s important is that you know if you need to have
Git ignore something, you can do it with the .gitignore
file.
Key Points
The
.gitignore
file tells Git what files to ignore.