.gitignore file - ignoring files in Git | Atlassian Git Tutorial (2024)

Git ignore patterns

.gitignore uses globbing patterns to match against file names. You can construct your patterns using various symbols:

Pattern

Example matches

Explanation*

**/logs

Example matches

logs/debug.loglogs/monday/foo.barbuild/logs/debug.log

Explanation*

You can prepend a pattern with a double asterisk to match directories anywhere in the repository.

**/logs/debug.log

Example matches

logs/debug.logbuild/logs/debug.logbut notlogs/build/debug.log

Explanation*

You can also use a double asterisk to match files based on their name and the name of their parent directory.

*.log

Example matches

debug.logfoo.log.loglogs/debug.log

Explanation*

An asterisk is a wildcard that matches zero or more characters.

*.log!important.log

Example matches

debug.logbut notlogs/debug.log

Explanation*

Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but also matches a negating pattern defined later in the file, it will not be ignored.

/debug.log

Example matches

debug.logbut notlogs/debug.log

Explanation*

Patterns defined after a negating pattern will re-ignore any previously negated files.

debug.log

Example matches

debug.loglogs/debug.log

Explanation*

Prepending a slash matches files only in the repository root.

debug?.log

Example matches

debug0.logdebugg.logbut notdebug10.log

Explanation*

A question mark matches exactly one character.

debug[0-9].log

Example matches

debug0.logdebug1.logbut notdebug10.log

Explanation*

Square brackets can also be used to match a single character from a specified range.

debug[01].log

Example matches

debug0.logdebug1.logbut notdebug2.logdebug01.log

Explanation*

Square brackets match a single character form the specified set.

debug[!01].log

Example matches

debug2.logbut notdebug0.logdebug1.logdebug01.log

Explanation*

An exclamation mark can be used to match any character except one from the specified set.

debug[a-z].log

Example matches

debuga.logdebugb.logbut notdebug1.log

Explanation*

Ranges can be numeric or alphabetic.

logs

Example matches

logslogs/debug.loglogs/latest/foo.barbuild/logsbuild/logs/debug.log

Explanation*

If you don't append a slash, the pattern will match both files and the contents of directories with that name. In the example matches on the left, both directories and files named logs are ignored

logs/

Example matches

logs/debug.loglogs/latest/foo.barbuild/logs/foo.barbuild/logs/latest/debug.log

Explanation*

Appending a slash indicates the pattern is a directory. The entire contents of any directory in the repository matching that name – including all of its files and subdirectories – will be ignored

logs/!logs/important.log

Example matches

logs/debug.loglogs/important.log

Explanation*

Wait a minute! Shouldn't logs/important.log be negated in the example on the leftNope! Due to a performance-related quirk in Git, you can not negate a file that is ignored due to a pattern matching a directory

logs/**/debug.log

Example matches

logs/debug.loglogs/monday/debug.loglogs/monday/pm/debug.log

Explanation*

A double asterisk matches zero or more directories.

logs/*day/debug.log

Example matches

logs/monday/debug.loglogs/tuesday/debug.logbut notlogs/latest/debug.log

Explanation*

Wildcards can be used in directory names as well.

logs/debug.log

Example matches

logs/debug.logbut notdebug.logbuild/logs/debug.log

Explanation*

Patterns specifying a file in a particular directory are relative to the repository root. (You can prepend a slash if you like, but it doesn't do anything special.)

** these explanations assume your .gitignore file is in the top level directory of your repository, as is the convention. If your repository has multiple .gitignore files, simply mentally replace "repository root" with "directory containing the .gitignore file" (and consider unifying them, for the sanity of your team).*

In addition to these characters, you can use # to include comments in your .gitignore file:

#ignorealllogs
*.log

You can use \ to escape .gitignore pattern characters if you have files or directories containing them:

#ignorethefileliterallynamedfoo[01].txt
foo\[01\].txt

.gitignore file - ignoring files in Git | Atlassian Git Tutorial (2)

SEE SOLUTION

Learn Git with Bitbucket Cloud

Read tutorial

Git ignore rules are usually defined in a .gitignore file at the root of your repository. However, you can choose to define multiple .gitignore files in different directories in your repository. Each pattern in a particular .gitignore file is tested relative to the directory containing that file. However the convention, and simplest approach, is to define a single .gitignore file in the root. As your .gitignore file is checked in, it is versioned like any other file in your repository and shared with your teammates when you push. Typically you should only include patterns in .gitignore that will benefit other users of the repository.

Personal Git ignore rules

You can also define personal ignore patterns for a particular repository in a special file at .git/info/exclude. These are not versioned, and not distributed with your repository, so it's an appropriate place to include patterns that will likely only benefit you. For example if you have a custom logging setup, or special development tools that produce files in your repository's working directory, you could consider adding them to .git/info/exclude to prevent them from being accidentally committed to your repository.

Global Git ignore rules

In addition, you can define global Git ignore patterns for all repositories on your local system by setting the Git core.excludesFile property. You'll have to create this file yourself. If you're unsure where to put your global .gitignore file, your home directory isn't a bad choice (and makes it easy to find later). Once you've created the file, you'll need to configure its location with git config:

$touch~/.gitignore
$gitconfig--globalcore.excludesFile~/.gitignore

You should be careful what patterns you choose to globally ignore, as different file types are relevant for different projects. Special operating system files (e.g. .DS_Store and thumbs.db) or temporary files created by some developer tools are typical candidates for ignoring globally.

Ignoring a previously committed file

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

$echodebug.log>>.gitignore

$gitrm--cacheddebug.log
rm'debug.log'

$gitcommit-m"Startignoringdebug.log"

You can omit the --cached option if you want to delete the file from both the repository and your local file system.

Committing an ignored file

It is possible to force an ignored file to be committed to the repository using the -f (or --force) option with git add:

$cat.gitignore
*.log

$gitadd-fdebug.log

$gitcommit-m"Forceaddingdebug.log"

You might consider doing this if you have a general pattern (like *.log) defined, but you want to commit a specific file. However a better solution is to define an exception to the general rule:

$echo!debug.log>>.gitignore

$cat.gitignore
*.log
!debug.log

$gitadddebug.log

$gitcommit-m"Addingdebug.log"

This approach is more obvious, and less confusing, for your teammates.

Stashing an ignored file

git stash is a powerful Git feature for temporarily shelving and reverting local changes, allowing you to re-apply them later on. As you'd expect, by default git stash ignores ignored files and only stashes changes to files that are tracked by Git. However, you can invoke git stash with the --all option to stash changes to ignored and untracked files as well.

Debugging .gitignore files

If you have complicated .gitignore patterns, or patterns spread over multiple .gitignore files, it can be difficult to track down why a particular file is being ignored. You can use the git check-ignore command with the -v (or --verbose) option to determine which pattern is causing a particular file to be ignored:

$gitcheck-ignore-vdebug.log
.gitignore:3:*.logdebug.log

The output shows:

<filecontainingthepattern>:<linenumberofthepattern>:<pattern><filename>

You can pass multiple file names to git check-ignore if you like, and the names themselves don't even have to correspond to files that exist in your repository.

Share this article

Next Topic
Inspecting a repository
.gitignore file - ignoring files in Git | Atlassian Git Tutorial (2024)

FAQs

.gitignore file - ignoring files in Git | Atlassian Git Tutorial? ›

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How to use gitignore to ignore files? ›

Configuring ignored files for a single repository
  1. Open Terminal .
  2. Navigate to the location of your Git repository.
  3. Create a .gitignore file for your repository. touch .gitignore. If the command succeeds, there will be no output.

How do I ignore changes to ignored files in git? ›

You can use various mechanisms to let Git know which files in your project not to track, and to ensure that Git won't report changes to those files. For files that Git doesn't track, you can use a . gitignore or exclude file. For files that Git does track, you can tell Git to stop tracking them and to ignore changes.

How do I find out why a file is ignored in git? ›

Your file might be git ignored. You can use the git check-ignore command to verify if your file is gitnored. It will tell you also which gitignore if you have multiple and which line. The -v is a verbose flag and gives us the line number and the actual .

Why does git ignore files? ›

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached to remove the file from the index.

Where to put .gitignore file? ›

gitignore file gets placed in the root directory of the repository. The root directory is also known as the parent and the current working directory. The root folder contains all the files and other folders that make up the project. That said, you can place it in any folder in the repository.

How do I ignore a specific folder in gitignore? ›

gitignore can also be used to ignore entire directories, along with any files and subdirectories in the directory. To ignore a specific directory, append a / symbol to the end of the directory name.

How to list all existing files ignored by git? ›

You can use git ls-files --others --ignored --exclude-standard to list untracked files that were ignored by the rules in your . gitignore .

What to write in gitignore? ›

Items to put in the . gitignore
  1. System-specific files. System-specific files need to get ignored. ...
  2. Vscode workspaces. Items like a vscode workspace need to be ignored.
  3. Security and API keys/secrets. For security, the security key files and API keys should get added to the gitignore.
Aug 5, 2020

How do I ignore all changes in git? ›

The easiest way to restore your working directory and discard any local changes is to issue a git stash command. This not only discards all local changes, but it also stores a record of your changes for future retrieval with a pop or apply command.

Why is my gitignore being ignored? ›

Incorrect Syntax or Patterns

Typos or incorrect syntax in the . gitignore file can render rules ineffective, leading to unintended tracking or ignoring of files. Solution: Carefully review and correct the syntax. Ensure that each pattern is on a new line for clarity.

How to make .gitignore take effect? ›

gitgnore file to ensure Git is always tracking the right files.
  1. Make changes in . gitignore file.
  2. Run git rm -r --cached . command.
  3. Run git add . command.
  4. git commit -m "Commit message" or just git commit or continue working.
Mar 11, 2020

How to create a gitignore file? ›

Step 1: Install the gitignore extension for VSCode. Step 2: Open the command palette using Ctrl+Shift+P and type Add gitignore. Step 3: Select the framework or the language of your project, and the extension will generate the . gitignore file for you.

What is the correct way to ignore files in git? ›

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

What is .gitignore file in git? ›

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details. Each line in a gitignore file specifies a pattern.

Does .gitignore need to be in root? ›

. gitignore files don't have to live at the root of the working directory: Patterns read from a .

How to hide a file using gitignore? ›

To create a Git Ignore File, save an empty file named . gitignore into the base folder of your Git repo using your code editor. Files starting with a period (.) are hidden in Unix-based operating systems like macOS.

Why files in gitignore are not ignored? ›

Files Are Already Tracked or Committed Before Being Added to Gitignore. Sometimes, you may add files to Gitignore after they have already been tracked or committed. In such cases, Git will continue to track those files even if they are listed in Gitignore.

How does gitignore work? ›

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple . gitignore files.

How do I remove a specific file from gitignore? ›

Remove the file from Git version control using the git rm command:
  1. git rm --cached file.txt.
  2. git commit -m "Remove file.txt from Git tracking"
  3. git add .gitignore git commit -m "Add file.txt to .gitignore"
Jun 6, 2023

Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 5926

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.