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

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Terminal Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Ignore and .gitignore


Share

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
Try it Yourself »

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
Try it Yourself »

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
rm 'filename.txt'
Try it Yourself »

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 specifying files in specific folders are always relative 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 with the .file extension /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) /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 specified in the set of characters (in this case a, b, or c) /names.file
/namex.file

no match:
/namesb.file
*.file All files with the .file extension /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 with the .file extension, 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 with the .file extension, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

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, cookies and privacy policy.

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