Signup/Sign In

Git .gitignore file

The .gitignore file is a text document that tells Git what files it should ignore. We don't always want Git to keep a track of every file on our repository and we can ask Git to ignore the changes that we make to our files with the help of a special .gitignore file.

.gitignore file containing files we don't want to share.

What is the .gitignore file?

  • We would often have many files in our repository which may be autogenerated by our software or we may even have some personal files whose content we don’t want to share.

  • The .gitignore file is a text file in which we can mention names or patterns to identify the files or folders that we want Git to ignore.

  • By ignoring we mean that Git should not track any changes made to these files.

  • We can create a single .gitignore file in our repository or multiple .gitignore files in different subdirectories of our project.

  • We can also create a global .gitignore file that will ignore the mentioned files in all the Git repositories on that system.

  • However, we have to commit the .gitignore file to make sure that if someone else clones this repository then that person also has the same ignore rules.

Git .gitignore File Patterns

We can add names of files or can specify patterns to identify certain files in the .gitignore file. Let's understand how to construct these patterns.

  1. # is used as a comment to increase the readability of the file.

  2. / symbol is used as a directory separator. If the pattern starts with a / then Git matches the files present only in the same directory as the .gitignore file. When a pattern ends with /, Git only matches the directory names. If a directory is ignored then all the content of that directory is ignored by Git.

  3. * matches zero or more characters.

  4. ** is used to match any file or zero or more directories. A / followed by ** is used to match zero or more directories inside that directory. A / after the ** matches the pattern in all the directories. A / followed by ** which is again followed by / matches zero or more directories.

  5. ? matches a single character.

  6. ! is used to undo the ignore pattern of a previous pattern. For example, the pattern *.txt covers the file with the name do_not_ignore.txt but if you add the negation sign like !do_not_ignore.txt then Git won't ignore this file.

  7. [ ] are used to match any one character in between the square brackets. A - between characters denotes the range. If after the opening bracket we have ! then the pattern matches any characters except those in between the brackets.

Let's look at some examples to better understand the usage of the above patterns.

Pattern Files/Directories Matched
/xyz.txt The file named xyz.txt in the root directory
abc/ All the content of the directory named abc like abc/xyz.txt, abc/def/file1.txt
*.py All files ending with .py like,
a*b.txt All files having the first letter a and ending with b.txt like acb.txt, ab.txt, accb.txt
**/xyz Matches the file or directory xyz anywhere in the repository
xyz/** Matches all the files or subdirectories inside the directory named xyz
xyz/**/abc Matches zero or more directories with paths like xyz/abc, xyz/def/abc, xyz/a/b/c/abc



Ignore all files with the .py extension except the file named
a?b.txt All files starting with a and ending with b.txt with exactly one letter in between like acb.txt
a??b.txt All files starting with a and ending with b.txt with exactly two letters in between like acdb.txt
abc[012].txt Files starting with abc and ending with .txt with only one of the letter from the square brackets like abc0.txt, abc1.txt, abc2.txt
abc[d-z].txt Files starting with abc and ending with .txt with only one letter in the range of d to z like abcd.txt, abct.txt
abc[!d].txt Files starting with abc and ending with .txt with any letter in between except d like abce.txt

How to Ignore?

As discussed in the above sections, we can tell Git to ignore files by either directly writing their names in the .gitignore file or by specifying patterns to identify a certain group of files.

To create a global .gitignore file we will have to configure it using the Git Config command. We can give any name to the global .gitignore file.

$ touch ~/.global_gitignore
$ git config --global core.excludesfile ~/.global_gitignore

Sometimes we may want to ignore a file that was previously committed. We can do this by first removing the file from the staging area and then adding the file name or pattern in the .gitignore file. Use the following command to remove the file from the staging area.

$ git rm --cached <file-name>

Similarly, we may also want to commit a file that was previously ignored. We can do this by forcefully add the file to the staging area by using -f along with the Git Add command.

$ git add -f <file-name>

We can also mention our personal ignore rules which are not committed by Git so they cannot be cloned by others. These rules can be for the files that may only be created by your system or by some software that you are using for the project. We can set these rules in the .git/info/exclude file present in the repository.


The .gitignore file is a very important part of version control as we don't always want Git to track all the files. These files can be the ones that are auto-generated by our project or some other files that are only created by our development tools that may lead to an error if cloned by someone else. We can construct different patterns in the .gitignore file to identify the files or directories that we want to ignore. We can also create a global .gitignore file for all our Git repositories. We also learned that we can commit an ignored file or can ignore a committed file. I hope this tutorial was helpful and you learned something new.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight