Git Ignore - Ignoring files in Git repository

Git

Share

In this tutorial we will learn to ignore files using the .gitignore file in Git repository.

What is .gitignore file?

This is the file that we add in our project folder and it tells Git to ignore files and directories that are mentioned.

Files from Git's perspective

Every file in the working directory of our project is seen by Git as one of the following.

  • Untracked
  • Tracked
  • Ignored

An untracked file is one that has never been staged or committed.

A tracked file is one that has been staged or committed earlier.

An ignored file is one that is ignored by Git as it is explicitly told to do so.

Why we use .gitignore file

As our project grows we will have multiple files and directories. In such a scenario it is not needed to save (commit) files and directories that are not of major importance.

For example, the hidden system files like .DS_Store or log files like .log and .lock and IDE config files like .idea/workspace.xml can be ignored as they are not the actual code of the project.

Similarly, build directories like dist/ or build/ which contains output files that can we regenerated every time we build our project can be easily ignored from the Git commit.

Content of .gitignore file

The .gitignore file contains some patterns that are matched with files in our project folder (working directory) and Git then decides whether or not to exclude (ignore) the files from commit based on pattern match.

Create the .gitignore file

Open the project folder in terminal and run touch .gitignore command and it will create the file. Or you can create a file from your IDE and save it as .gitignore file.

Patterns for .gitignore file

Following are some of the patterns that you can use in the .gitignore file.

Pattern Description Match
logs This will match both files and content of a directories with the name logs. So, files and directories by the name logs are ignored. logs
logs/user.log
dist/logs
dist/logs/error.log
logs/ A slash indicates that the pattern is a directory. So, directories matching the name logs and all its files and subdirectories will be ignored. logs/user.log
logs/build/error.log
dist/logs/error.log
dist/logs/user/debug.log
error.log By default, patterns match files so, this pattern will ignore any error.log file. error.log
logs/error.log
dist/logs/user/error.log
/error.log A leading slash will only match files in the repository root directory. Following Ignored
error.log

Following Not ignored
logs/error.log
logs/error.log This pattern will ignore error.log file having logs/ as its parent directory. And this is relative to the repository root. Following Ignored
logs/error.log

Following Not ignored
dist/logs/error.log
*.log An asterisk will match zero or more characters. So, the given pattern will ignore files ending with .log .log
error.log
logs/.log
logs/user.log
dist/logs/.log
dist/logs/user.log
*.log
!important.log
The prepending exclamation mark negates a pattern. So, in the given patterns all the files matching the .log pattern will be ignored but files matching the negating pattern important.log will not be ignored even though they match the .log pattern. Following Ignored
.log
error.log
logs/error.log

Following Not Ignored
important.log
logs/important.log
dist/logs/important.log
*.log
!src/*.log
temp.*
Patterns that are defined after a negated pattern re-ignores any previously negated files.
So, all files matching the .log pattern are ignored.
Then we have the negated pattern !src/*.log so, any file under the src/ directory matching the .log pattern is not ignored.
After the negated pattern we have a new pattern temp.* so, any file matching the temp. pattern is ignored even if it is inside the src/ directory and even if it ends with a .log
Following Ignored
.log
user.log
temp.log
temp.bak
logs/temp.log
src/temp.log

Following Not Ignored
src/.log
src/db.log
**/logs Leading double asterisk in a pattern matches directories anywhere in the repository. So, the given pattern will ignore any directory and all its files and subdirectories matching the pattern logs logs/error.log
logs/user/db.bak
dist/logs/error.log
**/logs/user.temp Leading double asterisk in a pattern with file name and parent directory can ignore all files matching that pattern. So, in the given pattern any user.temp file having logs as parent directory will be ignored. Following Ignored
logs/user.temp
dist/logs/user.temp

Following Not Ignored
dist/logs/user/user.temp
logs/**/user.temp Leading double asterisk with slash will match directory. So, in the given pattern any user.temp file directly inside or under subdirectories of logs directory will be ignored. logs/user.temp
logs/temp/user.temp
dist/logs/user.temp
dist/logs/user/user.temp
logs/*tmp/user.temp Leading asterisk will match directory name. So, in the given pattern any user.temp file under subdirectories having pattern tmp of logs directory will be ignored. Following Ignored
logs/user_tmp/user.temp
logs/tmp/user.temp
dist/logs/tmp/user.temp
dist/logs/usertmp/user.temp

Following Not Ignored
dist/logs/user/tmp/user.temp
dist/logs/user/dbtmp/user.temp
error?.log Question mark matches single character. So, the given pattern will match files having single character after error and before .log Following Ignored
error0.log
error1.log
errorA.log
errorB.log

Following Not Ignored
error10.log
errorAB.log
error[0-9].log We use the square bracket to match a single character in a given range. In the given pattern error can have 0 to 9 digit after it and before .log Following Ignored
error0.log
error1.log
error9.log

Following Not Ignored
errorA.log
error10.log
error[a-z].log We use the square bracket to match a single character in a given range. In the given pattern error can have a to z letters after it and before .log Following Ignored
errora.log
errorb.log
errorc.log

Following Not Ignored
error1.log
errorab.log
error[123].log We use the square bracket to match a single character in a given range. In the given pattern error can have 1, 2 and 3 digit after it and before .log Following Ignored
error1.log
error2.log
error3.log

Following Not Ignored
error0.log
errorA.log
error[!01].log Exclamation mark inside the square bracket is to match anything expect the characters in the bracket. In the given pattern error can have any other character after it and before .log but not 0 and 1. Following Ignored
error2.log
errorA.log
error99.log

Following Not Ignored
error0.log
error1.log