Back to Git & Dev Tools
2026-02-287 min read

gitignore[5] (Git & Dev Tools)

Learn gitignore[5] (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this full guide, we will delve into the essential topic of .gitignore, a powerful tool that helps developers manage untracked files within their projects using Git. By the end of this tutorial, you'll be able to create your own .gitignore files and avoid common pitfalls.

Why This Matters

In software development, it is crucial to keep your project clean and organized, especially when working with version control systems like Git. .gitignore plays a vital role in this process by excluding specific files or directories from being tracked by Git, preventing clutter and accidental commits of sensitive or unnecessary data.

Prerequisites

Before diving into the core concept, it's essential to have a basic understanding of:

  1. Git and its fundamental commands (e.g., git init, git add, git commit, git push, and git status)
  2. File systems and directories
  3. Basic text editing using tools like nano, vi, or Visual Studio Code
  4. Understanding the difference between tracked, untracked, and ignored files in Git

Core Concept

.gitignore is a file placed at the root of your project directory that lists patterns for files Git should intentionally ignore. Each line in this file specifies a pattern, allowing developers to exclude specific files or entire directories from version control.

Pattern Format

Each pattern in .gitignore follows a simple syntax:

  • A pattern can be a single filename, a directory name, or a glob pattern (a pattern that matches multiple filenames).
  • Glob patterns use special characters to match files with specific extensions or names. For example, *.txt will ignore all files ending in .txt, and node_modules/ ignores the entire node_modules directory.
  • Patterns are case-insensitive by default but can be made case-sensitive if the pattern starts with an exclamation mark (!). For example, !README.md will include only the file named README.md.

Configuration Notes

  • Git ignores files and directories based on patterns from multiple sources, with the following order of precedence:
  1. The project's root .gitignore file
  2. A global .gitignore file in your home directory (usually located at ~/.gitignore)
  3. The .gitignore files in parent directories, moving upwards from the project's root directory
  • To include a pattern that should be ignored only for the current repository, add it to a file named .git/info/exclude. This file is not committed to version control and will not affect other repositories.

Worked Example

Let's create a simple project with some untracked files and demonstrate how to use .gitignore effectively:

  1. Create a new directory for our project: mkdir my-project && cd my-project
  2. Add some sample files:
touch README.md log.txt .env
mkdir tmp
echo "Hello, World!" > tmp/hello.txt
  1. Initialize the Git repository and check its status: git init && git status
  2. Notice that all files are tracked (shown as unmodified):
On branch master

Initial commit

Untracked files:
(use "git add <file>..." to include in what will be committed)

README.md
log.txt
.env
tmp/
tmp/hello.txt
  1. Create a .gitignore file at the root of your project directory and add the following lines:
.env
tmp/*
*.log
  1. Add the tracked files to Git and commit the changes:
git add README.md log.txt .gitignore
git commit -m "Initial commit with .gitignore"
  1. Check the status again: git status
  2. Notice that the untracked files (.env, tmp/hello.txt) are no longer listed, as they have been ignored by Git based on the patterns specified in our .gitignore file.

Additional Worked Example - Ignoring Specific Files within a Directory

Let's say we want to keep the entire tmp directory tracked but ignore specific files within it:

  1. Modify the .gitignore file to include:
tmp/
!tmp/hello.txt
  1. Add and commit the changes:
git add .gitignore
git commit -m "Ignoring specific files in tmp directory"
  1. Check the status again: git status
  2. Notice that only the tmp/hello.txt file remains untracked, while the entire tmp directory is now tracked.

Common Mistakes

  1. Ignoring a directory without specifying its contents: If you ignore a directory using a pattern like dir/, any files or subdirectories within that directory will still be tracked unless explicitly ignored with additional patterns. For example, to ignore all files in the dir directory and its subdirectories, use dir/*.
  2. Case sensitivity: Remember that Git ignores patterns case-insensitively by default. To make a pattern case-sensitive, prefix it with an exclamation mark (e.g., !README.md).
  3. Ignoring essential files by accident: Be cautious when adding patterns to your .gitignore file, as accidentally ignoring important files can lead to issues down the line.
  4. Forgetting to add patterns for untracked files in .gitignore: Sometimes developers forget to add patterns for untracked files in their .gitignore file, causing those files to remain tracked despite being intended for exclusion.
  5. Ignoring files in the wrong order: If you have both a global .gitignore and a project-specific .gitignore, ensure that the project-specific file is placed at the root of your project directory to take precedence over the global one.
  6. Using absolute paths in .gitignore: When using absolute paths in your .gitignore file, make sure they are relative to the project's root directory instead of your home directory or system directories.
  7. Ignoring binary files without a wildcard pattern: If you want to ignore all binary files (e.g., log files, backup files) without explicitly listing each one, use a wildcard pattern like *~ or *.bak.
  8. Not committing .gitignore after initializing the repository: Remember to add and commit your .gitignore file after creating it, as Git will initially track all files in the project by default.

Practice Questions

  1. What is the purpose of a .gitignore file?
  2. How does Git determine which patterns to ignore in a .gitignore file?
  3. Can you explain the syntax for glob patterns in a .gitignore file?
  4. Why might it be important to make a pattern case-sensitive in your .gitignore file?
  5. What happens if you forget to add a pattern for an untracked file in your .gitignore file?
  6. How can you exclude specific files within a directory while keeping the directory tracked?
  7. What should you do if a file you want to track matches a pattern in your .gitignore file?
  8. Why is it important not to use absolute paths in your .gitignore file?
  9. How can you ignore all binary files without explicitly listing each one using glob patterns?
  10. What happens if you accidentally ignore an essential file by adding a pattern for it in your .gitignore file?

FAQ

  1. Can I ignore specific files within a directory while keeping the directory tracked?

Yes, you can use a pattern like dir/ to ignore the entire directory and then add individual exceptions using patterns like !dir/file1 !dir/file2.

  1. What happens if I add a new file that matches an existing pattern in my .gitignore file?

Git will automatically ignore the new file based on the pattern specified in your .gitignore file.

  1. Can I exclude specific files from being ignored for a single repository by using .git/info/exclude?

Yes, you can create a file named .git/info/exclude to include patterns that should be ignored only for the current repository.

  1. Is it possible to ignore all binary files in my project without explicitly listing each one?

Yes, you can use the wildcard pattern *~ to ignore all backup files (e.g., *.bak, *.swp, etc.).

  1. What should I do if a file I want to track matches a pattern in my .gitignore file?

You can either remove the pattern from your .gitignore file or add the file explicitly using git add .

  1. Is it possible to ignore specific files within a directory while keeping the directory tracked and the ignored files committed locally but not pushed to remote repositories?

Yes, you can use the git update-index --assume-unchanged command on individual files to prevent them from being tracked by Git without adding them to your .gitignore file. This allows you to keep the files tracked locally while ignoring them for commits and pushes. However, this should be used cautiously, as it can lead to issues if the untracked files are needed for collaboration or project consistency.

  1. What should I do if I accidentally ignore an essential file by adding a pattern for it in my .gitignore file?

You can remove the offending pattern from your .gitignore file, add and commit the ignored file using git add , then force Git to recognize the changes with git commit -amend. Alternatively, you can use git check-ignore to find the offending pattern and manually override it for the affected files by removing them from your .gitignore file or temporarily moving them out of the project directory before committing.

gitignore[5] (Git & Dev Tools) | Git & Dev Tools | XQA Learn