Do More with .gitignore


If you have worked with Git before, you may be familiar with the .gitignore file. Gitignore is a tool that lets you intentionally ignore untracked files, and it’s an easy way to avoid committing files from your working directory that are not useful to other collaborators, such as compiled files (*.o, *.pyc). It makes it easier to scan for new files or changes that you want to commit to the project. In this post, I will look at leveraging gitignore in more detail.

Getting Started

A common place to create your .gitignore file is at the root of your Git repository.

Once you have created the file, you can apply the patterns for files you want to ignore (see below, or check the documentation). In order for the patterns to apply, you would need to add and commit the .gitignore file with the following commands:

$ git add .gitignore
$ git commit -m ".gitignore patterns applied"

Although the .gitignore changes are applied locally, the .gitignore file is generally used to share ignored patterns with other developers. To apply the changes to other clones of this project, push the changes in .gitignore to the remote repository.

Level of Precedence

While placing the .gitignore file at root level of the Git project is one way to ignore untracked files, Git checks for ignore patterns from multiple sources. Git evaluates patterns based on the following order, with the last one dictating the outcome.

  1. .gitignore file: These patterns are applied to every clone of the repository. If there is a .gitignore file in the same directory containing the file, that .gitignore will be evaluated first; otherwise it will look to the parent directory for the .gitignore file.
  2. .git/info/exclude: These are patterns that apply only to your local copy of the repository and are not seen by other developers.
  3. core.excludesFile: All patterns in this file are applied globally across all Git repositories on your computer. You need to set up your global core.excludesfile configuration file to point to the global ignore file by creating the file at ~/.gitignore_global and then configure it with the following command:
    $ git config --global core.excludesfile ~/.gitignore_global

Some Best Practices for .gitignore

As mentioned before, it is generally good practice to include output files, log files, and any other files that let other developers avoid commits in the .gitignore file. It is also important to consider which file to place ignore patterns in. For instance, you should not use the .gitignore files in a repository to ignore files that only appear for you, such as editor-specific swap files; instead, consider using core.excludesFile.

Debugging .gitignore

Sometimes a pattern is formatted in a way that unintentionally excludes files. You can debug and see what is ignored with git-check-ignore:

$ git check-ignore [options] pathname

The -v, –verbose option is really useful to see which line in the .gitignore file caused a certain file to be ignored. In the example below, line 1 in the .gitignore file causes hello.o to be ignored in the current path.

$ cat .gitignore
$ git check-ignore -v *
.gitignore:1:*.o	hello.o

Untrack Files to Ignore

To stop tracking a file in order for it to be read by gitignore, use the following command:

$ git rm --cached  

Pattern Format

Here is an overview of .gitignore pattern formatting.

Final Note

There are existing .gitignore files out there for a variety of languages and frameworks. Gitignore may be worth looking at as a starting template, or as a means to examine best practices that you may have previously missed. You can find templates at the /github/gitignore repo or you can generate one at There are many considerations involved in creating a .gitignore file, and I hope what has been presented here serves as a good starting point for you going forward in your projects.


Gitignore Documentation
check-ignore Documentation
Useful gitignore templates

Lisa Leung has experience as a technical consultant prior to becoming a full-stack engineer student at Holberton School in San Francisco. She enjoys coding, gaming, and reading.


Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to toolbar