Menu
×
     ❯   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Git Ignore and .gitignore


What is .gitignore?

The .gitignore file tells Git which files and folders to ignore (not track).

This is useful for keeping log files, temporary files, build artifacts, or personal files out of your repository.

  • Examples of files to ignore: log files, temporary files, hidden files, personal files, OS/editor files, etc.

The .gitignore file itself is tracked by Git, so everyone using the repository ignores the same files.


When to Use .gitignore

  • When you want to keep sensitive, local, or unnecessary files out of your repository
  • When sharing a project with others and want to avoid cluttering Git history
  • When working with build tools or editors that create extra files

Create a .gitignore File

  1. Go to the root of your local Git repository.
  2. Create a file named .gitignore:

Example

touch .gitignore

Ignoring Folders

To ignore a folder and everything inside it, use a trailing slash:

temp/ 

This ignores any folder named temp anywhere in your project.



Wildcards & Patterns

Wildcards let you match many files or folders at once:

  • * matches any number of characters
  • ? matches a single character
  • [abc] matches any character in the set
  • [!abc] matches any character not in the set
*.tmp      # all .tmp files
my?ile.txt # matches my1ile.txt, myAile.txt, etc.
log[0-9].txt # log1.txt, log2.txt, ... log9.txt 

Negation (!)

Use ! to not ignore something that would otherwise be ignored. This is called an exception:

*.log
!important.log 

This ignores all .log files except important.log.


Comments and Blank Lines

Lines starting with # are comments and are ignored by Git. Blank lines are also ignored. Use comments to explain your rules:

# Ignore log files
*.log

# Ignore temp folders
temp/ 

Local & Personal Ignore Rules

If you want to ignore files only for yourself (not for everyone who uses the repository), add them to .git/info/exclude. This works just like .gitignore but is not shared.


Global .gitignore (User Level)

You can set up a global .gitignore file for all your projects. This is great for ignoring OS or editor files everywhere (like .DS_Store or Thumbs.db):

git config --global core.excludesfile ~/.gitignore_global

Then add your patterns to ~/.gitignore_global.


How to Stop Tracking a File

If you add a file to .gitignore but Git is still tracking it, you need to tell Git to stop:

git rm --cached filename.txt

This removes the file from the repository but keeps it on your computer. Next time you commit, Git will ignore it.


Tips & Troubleshooting

  • Check for typos—.gitignore is case-sensitive!
  • If a file is already tracked, use git rm --cached to stop tracking it.
  • Use comments (#) to explain tricky rules for your teammates.
  • Use git status to see if your ignored files are being tracked.
  • Remember: .gitignore only affects files that are not already tracked by Git.

Pattern Syntax

Here are some common patterns and how they match:

Pattern Explanation/Matches Examples
  Blank lines are ignored  
# text comment Lines starting with # are ignored  
name All name files, name folders, and files and folders in any name folder /name.log
/name/file.txt
/lib/name.log
name/ Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder /name/file.txt
/name/log/name.log

no match:
/name.log
name.file All files with the name.file /name.file
/lib/name.file
/name.file Starting with / specifies the pattern matches only files in the root folder /name.file

no match:
/lib/name.file
lib/name.file Patterns specifiing files in specific folders are always realative to root (even if you do not start with / ) /lib/name.file

no match:
name.file
/test/lib/name.file
**/lib/name.file Starting with ** before / specifies that it matches any folder in the repository. Not just on root. /lib/name.file
/test/lib/name.file
**/name All name folders, and files and folders in any name folder /name/log.file
/lib/name/log.file
/name/lib/log.file
/lib/**/name All name folders, and files and folders in any name folder within the lib folder. /lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file
*.file All files withe .file extention /name.file
/lib/name.file
*name/ All folders ending with name /lastname/log.file
/firstname/log.file
name?.file ? matches a single non-specific character /names.file
/name1.file

no match:
/names1.file
name[a-z].file [range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.) /names.file
/nameb.file

no match:
/name1.file
name[abc].file [set] matches a single character in the specified set of characters (in this case either a, b, or c) /namea.file
/nameb.file

no match:
/names.file
name[!abc].file [!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c) /names.file
/namex.file

no match:
/namesb.file
*.file All files withe .file extention /name.file
/lib/name.file
name/
!name/secret.log
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log /name/file.txt
/name/log/name.log

no match:
/name/secret.log
*.file
!name.file
! specifies a negation or exception. All files withe .file extention, except name.file /log.file
/lastname.file

no match:
/name.file
*.file
!name/*.file
junk.*
Adding new patterns after a negation will re-ignore a previous negated file
All files withe .file extention, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file

Exercise?What is this?
Test your skills by answering a few questions about the topics of this page

Drag and drop the correct code to ignore all .log files in .gitignore.
*.log
!important.log
log.txt




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.