DEV Community

Vuyisile Ndlovu
Vuyisile Ndlovu

Posted on • Originally published at vuyisile.com on

Git: Ignoring Files

Git can be configured to ignore some files in your computer or repository you don’t want to track. These files can include log files, .env files that contain sensitive information, files generated automatically by your text editor or build system, or any locally generated files that your team members will not need.

You use gitignore files to tell git what files to ignore. Once a file is already tracked, adding it to gitignore won’t un-track it. Each line in the gitignore file represents a file name pattern that git will try to match. Here is an example gitignore file:

$ cat .gitignore
*.[oa]
*~
Enter fullscreen mode Exit fullscreen mode

The first line tells git to ignore all files ending in .o or .a. The second line tells git to ignore all files that end with a tilde(~), which many editors use to mark temp files. Setting up a gitignore file early in your project is a good practice, so you don’t accidentally add files you don’t really want to your git repository.

Git usually checks for these patterns in multiple locations and where you place the patterns depends on how you want to use the patterns.

Ignoring files in a single repository

You can tell git to ignore files in a repository or project by adding a .gitignore file in the project’s root. This file tells git what files and folders to ignore when you make a commit. To share this file with others, commit it to git. If you want to ignore a file that is already tracked or checked in, you must untrack the file first before ignoring it:

$ git rm --cached FILENAME
Enter fullscreen mode Exit fullscreen mode

Ignoring files in all repositories

You can create a global gitignore file to define a list of rules or files to ignore for every project on your computer. To do this, you create a gitignore file and configure git to use it globally. For example, you could create .gitignore_global in your home directory and then do this:

$ git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

Excluding files without creating a .gitignore file

If you don’t want to share a .gitignore file or want to add additional rules that are specific to your workflow, you can create a set of rules to ignore that don’t get committed to the repository by editing the file in .git/info/exlude within the root of your git project and adding the patterns there.

Gitignore pattern examples

Pattern Description
*.oa Ignore .o or .a files
# This or a blank line is treated as a comment or space for readability.
\# Ignore patterns that start with a hash
/ Start a pattern with a forward slash to avoid recursivity. End patterns with
a slash to specify a directory.
/TODO Ignore the TODO file in *this* directory only
build/ Ignore all files in any directory called build
! Negates the pattern that comes after it
? Matches any one character except for /
[0-9] Range notation; matches any digit between 0and 9
[a-z] Range notation; matches any character between a and z
a/**/z Match any nested directories. e.g a/z, a/b/z, a/b/c/z
*.a Ignore( or don’t track) any .a files
!lib.a Do not ignore the lib.a file
doc/*.txt This will ignore /doc/notes.txt but not doc/server/arch.txt
doc/**/*.pdf Ignore .pdf files in the doc directory and any of its subdirectories.

Conclusion

This post was a quick summary of how to ignore files in git using .gitignore files and some of the patterns you can use in them. GitHub maintains a list of .gitignore templates you can use in your projects, so you don’t have to come up with a list of rules every time you create a project.

Further Reading:


Top comments (0)