Ignoring in Git: How to Use. Gitignore Files

When making commits to any Git repository, you’ll choose the files you want to stage and then you’ll commit them.

But you might not want to commit every single one of your files—there are files that never need to get committed. This is where the .gitignore file is useful: it tells Git exactly which files to ignore and never track.

Let’s take a look.

What is gitignore?

When you’re working in your copy, Git watches every file in and considers it in three ways:

  1. Tracked: You’ve already staged or committed the file.
  2. Untracked: You’ve not staged or committed.
  3. Ignored: You’ve explicitly told Git to ignore the file(s).

The .gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo.

The .gitignore file itself is a plain text document. Here’s an example .gitignore file:

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
vendor/ 
  • * is used as a wildcard match *.exe will ignore any file with the .exe extension
  • / will ignore directories with the name. vendor/ ignores the vendor directory.
  • # will comment the line
  • […] will ignore values with any of the values.
    • *.[abc] ignores files file.a, file.b, file.c.
    • *.[a-*.[oa]d] the dash will include a range, in this case, file extensions a-d.

Why do I need to ignore files in Git?

You may want to ignore certain files for multiple reasons:

  • The files contain sensitive data.
  • The files are system specific and do not need to exist on every machine’s copy.
  • Excluding the files maintains system security rules and privileges. (Remember, Git repos only contain the files necessary to get tech support—not to share the entire software.)

Julien Danjou points out there is a global .ignore file for your computer to ignore for every commit:

Not everybody uses your editor or favorite pet tool, and nobody cares. The repository you’re working in is shared with a lot of other developers. Sending pull requests to just add this kind of entry to ignore files generated by your pet editor is wrong and annoying.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article