Back
GitoryxGitoryx

Gitignore: Keep Files Out of Your Repository

A .gitignore file tells Git which files never to track. This tutorial covers pattern syntax, how to untrack files that were already committed, debugging rules, and using a global ignore for personal files.

Beginner7 min readUpdated

On this page

What is a .gitignore file?

A .gitignore file is a plain text file at the root of a repository that lists patterns of files and folders Git should ignore. Ignored files are not shown as untracked and are never staged by git add. It has no effect on files Git already tracks.

The short version

  1. 1Create .gitignore at the repository root
  2. 2Add patterns such as node_modules/, .env, and dist/
  3. 3Commit the file: git add .gitignore && git commit -m "Add .gitignore"
  4. 4Untrack already committed files: git rm --cached <file>
  5. 5Debug a rule: git check-ignore -v <file>

Why it matters

Your git status is flooded with node_modules, .DS_Store, and build output. Or worse, you committed a .env file with real credentials and only noticed after pushing.

A .gitignore file tells Git which files to never track. It keeps repositories small, diffs readable, and secrets out of history. Setting it up before the first commit is far easier than cleaning up afterwards.

Where it usually goes wrong

  • Adding a file to .gitignore after it is already tracked, and wondering why Git still shows it
  • Misplacing a slash so a pattern matches too much or too little
  • Ignoring a file that was already committed with secrets, assuming that removes it from history
  • Committing OS and editor files (.DS_Store, .idea/) that belong in a global ignore

Gitignore, step by step

  1. Step 1: Create the .gitignore file

    Create a file named .gitignore at the root of the repository and commit it so the whole team shares the same rules.

    Terminal
    touch .gitignoregit add .gitignoregit commit -m "Add .gitignore"

    Tip: GitHub's github/gitignore repository has ready-made templates for almost every language and framework.

  2. Step 2: Write patterns

    One pattern per line. A trailing / matches folders only, * is a wildcard, # starts a comment, and a leading ! re-includes something.

    .gitignore
    # Dependenciesnode_modules/# Build outputdist/*.log# Secrets.env.env.*!.env.example# OS files.DS_Store
  3. Step 3: Understand where patterns apply

    A leading / anchors a pattern to the folder containing the .gitignore. Without it, the pattern matches at any depth. ** matches across folders.

    .gitignore
    # Only the build folder at the root/build# Any folder named cache, anywherecache/# Any .tmp file under any docs folderdocs/**/*.tmp
  4. Step 4: Stop tracking a file that is already committed

    Adding a pattern does nothing to files Git already tracks. Remove them from the index (not from disk) with --cached, then commit.

    Terminal
    git rm --cached .envgit rm -r --cached node_modulesgit commit -m "Stop tracking ignored files"

    Careful: The file stays in your history. If it contained a secret, rotate the secret: ignoring it is not enough.

    Gitignore: Keep Files Out of Your Repository: the same workflow in Gitoryx
    In Gitoryx: Right-click an untracked file to add it, its folder, or its extension to .gitignoreHow it works
  5. Step 5: Find out why a file is ignored

    When a file will not show up, git check-ignore -v prints the exact rule and file responsible.

    Terminal
    git check-ignore -v path/to/file# .gitignore:3:dist/    dist/bundle.js
  6. Step 6: Use a global ignore for personal files

    Editor and OS files belong to you, not to the project. Put them in a global ignore file so you never have to add them to every repository.

    Terminal
    git config --global core.excludesfile ~/.gitignore_globalecho ".DS_Store" >> ~/.gitignore_globalecho ".idea/" >> ~/.gitignore_global

Mistakes to avoid with gitignore

Expecting .gitignore to untrack files
.gitignore only affects untracked files. A file already committed stays tracked, and changes to it keep showing up.
Fix: Run git rm --cached <file> and commit. The file stays on disk but leaves the index.
Believing ignoring a secret makes it safe
If a secret was ever committed, it remains in history and in every clone, even after you ignore and untrack the file.
Fix: Rotate the credential immediately, then rewrite history if needed.
Ignoring a folder, then trying to re-include a file inside it
Git cannot re-include a file if its parent folder is ignored: !dist/keep.txt has no effect after dist/.
Fix: Ignore the contents instead: dist/* then !dist/keep.txt.

Gitignore in Gitoryx

In Gitoryx, ignored files disappear from the changes list, and you can add any untracked file to .gitignore straight from its context menu.

  • Right-click an untracked file to add it, its folder, or its extension to .gitignore
  • Ignored files stay out of the changes list, so you only review what matters
  • Spot an accidentally tracked file in the diff and untrack it in one action
  • Edit .gitignore and see the effect on the file list straight away

Free plan · no account required · macOS, Windows and Linux

Gitignore: frequently asked questions

Where should the .gitignore file go?
At the root of the repository for project-wide rules. You can also add .gitignore files in subfolders; their patterns apply to that folder and below.
Why is Git still tracking a file I added to .gitignore?
Because it was committed before. Run git rm --cached <file> and commit; after that the ignore rule takes effect.
How do I ignore a file only on my machine?
Add it to .git/info/exclude, which works like .gitignore but is never committed or shared.
How do I see all ignored files?
Run git status --ignored or git ls-files --others --ignored --exclude-standard.
Can I commit an ignored file anyway?
Yes, with git add -f <file>. Use it rarely, as it usually signals a rule that needs adjusting.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64