Skip to content

Ignore Files (.gitignore)


What Will We Learn?

How to use .gitignore and what are the common patterns to use it.

What is .gitignore?

To put it simple, .gitignore file is a way of telling Git — “Don’t track these files or directories which I listed here.” This means the file patterns you include in gitignore file won’t be included in any part of your version control.

Why to Use .gitignore?

By default, Git will track all the files in your project but tracking, adding and committing all files can be security risk, unnecessary and can certainly become a mess while the project grows. You want gitignore because it filters your requirements:

  • You only want to include necessary and related project files
  • You don’t wanna include all temporary files used and generated developing any project
  • You certainly don’t wanna commit password or environment files that may expose security risk

How to Use .gitignore?

Start by creating the .gitignore file in the root of your project directory. The basic syntax is:

PatternMeaning
abcIgnore any file or directory named abc anywhere in the repository.
abc/Ignore any directory named abc anywhere in the repository (but not a file named abc).
/abcIgnore only the file or directory named abc in the repository root.
/abc/Ignore only the directory named abc in the repository root.
file.txtIgnore any file or directory named file.txt anywhere in the repository.
*.logIgnore all .log files anywhere in the repository.
*.tmpIgnore all .tmp files.
/file.txtIgnore only file.txt in the repository root.
**/dir/Ignore directories named dir at any depth (usually equivalent to dir/).
**/*.logIgnore all .log files recursively in every directory.
logs/*.logIgnore .log files directly inside any logs directory.
/logs/*.logIgnore .log files only in the root logs directory.
foo/**Ignore everything inside foo recursively, but not necessarily foo itself.
**/cache/Ignore any directory named cache anywhere in the repository.
!file.txtRe-include file.txt if it was ignored by a previous rule.
!dir/Re-include the dir directory if it was ignored by a previous rule.
# This is a commentComment; ignored by Git.
\#fileIgnore a file literally named #file.
\!important.txtIgnore a file literally named !important.txt.
[ab].txtIgnore a.txt and b.txt.
[0-9].logIgnore log files with a single-digit name, such as 1.log or 7.log.
file?.txtIgnore files like file1.txt or fileA.txt, but not file10.txt.

Tip

.gitignore supports common regex patterns as seen in the above table used for complex filtering.

Note

The line starting with # is a comment and ignored by Git.

Common Patterns

Common patterns include ignoring different patterns:

Ignoring IDE-Specific Files

IDE often creates config which are useful for IDE but not for your project and you should ignore them. For example ignore vscode config:

# vscode
.vscode/

Ignoring Temporary Files

You or your project may create temporary files. Ignore them:

*.tmp
*.bak
*.temp

Negation Pattern

If you wanna ignore all log files but not one, you can:

# ignore all log files
*.log
# but not app.log
!app.log

Ignoring Object and Executables Files

While working on C/C++ projects, compiling them creates build artifacts which are not necessary for source code:

/build/
*.exe
*.o

But if you wish to release executable in Github or elsewhere, you may use negation for certain executable like your app.exe:

!app.exe

.gitignore Tips

Tip

If file is already been tracked (currently staged) by Git, simply adding it to .gitignore will not just untrack it. You must unstage it first using git rm --cached <file>

Tip

You can check whether certain git ignore rule is being applied to certain file using git check-ignore -v <file>

Last updated on