Ignoring Things

Overview

Teaching: 5 min
Exercises: 0 min
Questions
  • 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:

Save empty files called

and see what GitHub Desktop says.

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.

There are two ways to do this:

Ignore Specific Files or File Types

If we want to ignore specific files, or files of a specific type, we can right-click on the file and choose “Ignore” or “Ignore all .dat files.”

dat and out files

 

Notice that doing that created a new file to be tracked called .gitignore. This file is invisible in your file browser, but Git and GitHub Desktop are using it to keep track of what files to ignore, and changes to it need to be added and committed, or they won’t be tracked.

Ignore Whole Directories

If we want to ignore all the files in a given directory (it’s common to ignore files that can be re-generated from code, like results files), we can do that by manually editing .gitignore in the repository settings.

Repo Settings

 

Under the “Ignore” tab, add a line results/*. The * in that expression is called a wildcard. It says that any file that starts with results/ (that is, any file inside the results directory) should be ignored.

Ignore Directory

 

Save that and you’ll notice that the new line is added to your .gitignore changes, but the two files in results/ have disappeared from your list of changes. Git is now ignoring them.

Go ahead and commit your .gitignore file.

Including Specific Files

How would you ignore all .data files in your root directory except for final.data? Hint: Find out what ! (the exclamation point operator) does

Solution

You would add the following two lines to your .gitignore:

*.data           # ignore all data files
!final.data      # except final.data

The exclamation point operator will include a previously excluded entry.

The Order of Rules

Given a .gitignore file with the following contents:

*.data
!*.data

What will be the result?

Solution

The ! modifier will negate an entry from a previously defined ignore pattern. Because the !*.data entry negates all of the previous .data files in the .gitignore, none of them will be ignored, and all .data files will be tracked.

Log Files

You wrote a script that creates many intermediate log-files of the form log_01, log_02, log_03, etc. You want to keep them but you do not want to track them through git.

  1. Write one .gitignore entry that excludes files of the form log_01, log_02, etc.

  2. Test your “ignore pattern” by creating some dummy files of the form log_01, etc.

  3. You find that the file log_01 is very important after all, add it to the tracked files.

  4. 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 .gitignore.

Solution

  1. append either log_* or log* as a new entry in your .gitignore
  2. track log_01 by adding the line !log_01 to your .gitignore after the log_* entry. .gitignore entries are evaluated in order.

Key Points

  • Ignoring files reduces visual clutter and makes you less error-prone.

  • Groups of files can be ignored all at once, and you can make exceptions to those groups.