Git sees every file in your working copy as one of three things:
- tracked - a file which has been previously staged or committed;
- untracked - a file which has not been staged or committed; or
- ignored - a file which Git has been explicitly told to ignore.
Ignored files are usually build artifacts and machine generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:
- dependency caches, such as the contents of
- compiled code, such as
- build output directories, such as
- files generated at runtime, such as
- hidden system files, such as
- personal IDE config files, such as
Ignored files are tracked in a special file named
.gitignore that is checked in at the root of your repository. There is no explicit git ignore command: instead the
.gitignore file must be edited and committed by hand when you have new files that you wish to ignore.
.gitignore files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored.
- Ignoring files in Git
Git ignore patterns
.gitignore uses globbing patterns to match against file names. You can construct your patterns using various symbols:
| || ||You can prepend a pattern with a double asterisk to match directories anywhere in the repository.|
| || |
|You can also use a double asterisk to match files based on their name and the name of their parent directory.|
| || ||An asterisk is a wildcard that matches zero or more characters.|
| || |
|Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but also matches a negating pattern defined later in the file, it will not be ignored.|
| || |
|Patterns defined after a negating pattern will re-ignore any previously negated files.|
| || |
|Prepending a slash matches files only in the repository root.|
| || ||By default, patterns match files in any directory|
| || |
|A question mark matches exactly one character.|
| || |
|Square brackets can also be used to match a single character from a specified range.|
| || |
|Square brackets match a single character form the specified set.|
| || |
|An exclamation mark can be used to match any character except one from the specified set.|
| || |
|Ranges can be numeric or alphabetic.|
| || ||If you don't append a slash, the pattern will match both files and the contents of directories with that name. In the example matches on the left, both directories and files named logs are ignored|
|logs/|| ||Appending a slash indicates the pattern is a directory. The entire contents of any directory in the repository matching that name – including all of its files and subdirectories – will be ignored|
| || || Wait a minute! Shouldn't |
Nope! Due to a performance-related quirk in Git, you can not negate a file that is ignored due to a pattern matching a directory
| || ||A double asterisk matches zero or more directories.|
| || |
|Wildcards can be used in directory names as well.|
| || |
|Patterns specifying a file in a particular directory are relative to the repository root. (You can prepend a slash if you like, but it doesn't do anything special.)|
** these explanations assume your .gitignore file is in the top level directory of your repository, as is the convention. If your repository has multiple .gitignore files, simply mentally replace "repository root" with "directory containing the .gitignore file" (and consider unifying them, for the sanity of your team).*
In addition to these characters, you can use # to include comments in your
# ignore all logs *.log
You can use \ to escape
.gitignore pattern characters if you have files or directories containing them:
# ignore the file literally named foo.txt foo\[01\].txt
Shared .gitignore files in your repository
Git ignore rules are usually defined in a
.gitignore file at the root of your repository. However, you can choose to define multiple
.gitignore files in different directories in your repository. Each pattern in a particular
.gitignore file is tested relative to the directory containing that file. However the convention, and simplest approach, is to define a single
.gitignore file in the root. As your
.gitignore file is checked in, it is versioned like any other file in your repository and shared with your teammates when you push. Typically you should only include patterns in
.gitignore that will benefit other users of the repository.
Personal Git ignore rules
You can also define personal ignore patterns for a particular repository in a special file at
.git/info/exclude. These are not versioned, and not distributed with your repository, so it's an appropriate place to include patterns that will likely only benefit you. For example if you have a custom logging setup, or special development tools that produce files in your repository's working directory, you could consider adding them to
.git/info/exclude to prevent them from being accidentally committed to your repository.
Global Git ignore rules
In addition, you can define global Git ignore patterns for all repositories on your local system by setting the Git
core.excludesFile property. You'll have to create this file yourself. If you're unsure where to put your global
.gitignore file, your home directory isn't a bad choice (and makes it easy to find later). Once you've created the file, you'll need to configure its location with
$ touch ~/.gitignore $ git config --global core.excludesFile ~/.gitignore
You should be careful what patterns you choose to globally ignore, as different file types are relevant for different projects. Special operating system files (e.g.
thumbs.db) or temporary files created by some developer tools are typical candidates for ignoring globally.
Ignoring a previously committed file
If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a
.gitignore rule for it. Using the
--cached option with
git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.
$ echo debug.log >> .gitignore $ git rm --cached debug.log rm 'debug.log' $ git commit -m "Start ignoring debug.log"
You can omit the
--cached option if you want to delete the file from both the repository and your local file system.
Committing an ignored file
It is possible to force an ignored file to be committed to the repository using the
--force) option with
$ cat .gitignore *.log $ git add -f debug.log $ git commit -m "Force adding debug.log"
You might consider doing this if you have a general pattern (like
*.log) defined, but you want to commit a specific file. However a better solution is to define an exception to the general rule:
$ echo !debug.log >> .gitignore $ cat .gitignore *.log !debug.log $ git add debug.log $ git commit -m "Adding debug.log"
This approach is more obvious, and less confusing, for your teammates.
Stashing an ignored file
git stash is a powerful Git feature for temporarily shelving and reverting local changes, allowing you to re-apply them later on. As you'd expect, by default
git stash ignores ignored files and only stashes changes to files that are tracked by Git. However, you can invoke git stash with the --all option to stash changes to ignored and untracked files as well.
Debugging .gitignore files
If you have complicated
.gitignore patterns, or patterns spread over multiple
.gitignore files, it can be difficult to track down why a particular file is being ignored. You can use the
git check-ignore command with the
--verbose) option to determine which pattern is causing a particular file to be ignored:
$ git check-ignore -v debug.log .gitignore:3:*.log debug.log
The output shows:
You can pass multiple file names to
git check-ignore if you like, and the names themselves don't even have to correspond to files that exist in your repository.