How to ignore files and directories in Git using a .gitignore

A .gitignore is a file that tells Git which files to ignore.

Git sees files in your directory 3 ways:

  1. Tracked
  2. Untracked
  3. Ignored

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

How to create and use a .gitignore file

We can create a .gitignore with the following command:

touch .gitignore

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 .gitignore using echo:

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 /a.out in .gitignore will ignore a file a.out in the same directory as .gitignore. /part1/a.out 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:

a.out
!/part1/a.out

Anything named “a.out” will be ignored by Git except a file a.out in the part1 directory.

Files already tracked by Git will not be affected by changes to .gitignore. 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.

How to ignore files using globbing patterns

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 the pattern.

* is a wildcard and matches with any characters, zero or more, except /. For example, we can use *.o to ignore any files ending with .o, such as hello.o bye.o and .o.

We can use ? to match with any one character except /. If our directory has many files called test1.c, test2.c, test3.c, test4.c, etc. We can ignore all these files with the patten test?.c.

We can also use square brackets [] to match one character to a set of characters within the brackets. If we have files, testA.c, testB.c and testC., that we want to ignore, we can use the pattern test[ABC].c

We can use range notation to make this more succinct. The pattern test[A-C].c will ignore the same files as test[ABC].c. Both alphabetical and numeric ranges can be used within square brackets. The pattern test[A-C0-9].c will match with both testA.c and test0.c but not test11.c.

While not a perfect analogy, ** can be used with directories similarly to how * 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 astmp/hello.o and tmp/part1/bye.o.

/** will match with anything inside a directory. /ignore/** will match with all files in the ignore directory.

**/ matches all directories. **/test* matches any file beginning with “test” and is equivalent to test*

Debugging

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.


Acknowledgements

This guide was created by Chloe Ho using the following resources:

https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files

https://www.atlassian.com/git/tutorials/saving-changes/gitignore

https://www.pluralsight.com/guides/how-to-use-gitignore-file

https://man7.org/linux/man-pages/man7/glob.7.html#:~:text=Globbing%20is%20the%20operation%20that,string%2C%20including%20the%20empty%20string.

Last updated: 2022-11-08