Skip to content

Ignoring files in git

Configure Git to prevent certain files from being committed.

Git generally examines each file in a repository for changes and adds them to the change list. It wants to be as content-agnostic and makes as few assumptions about your environment as possible, so it doesn't try to guess which files you want to put under version control and which you don't want to.

Fortunately, git offers the possibility to ignore/exclude files or directories. If these ignored files are present in your working directory and you edit them, they're hidden from the change list and can't be accidentally committed anymore.

Why and why not to ignore?

These are typical reasons for ignoring files:

  • Eliminate redundancy in the history (build artefacts, processed data)
  • Get rid of distracting intermediate files specific to your working environment and not valuable for your project collaborators (cache, logs, temp files)
  • Avoid wasting disk space through duplication (build dependencies)

Reasons against ignoring can be:

  • A build or processing step is very slow or resource-hungry
  • Some of your build dependencies cannot be referenced precisely by their version. If reproducibility is a big concern for you, it might be preferable to commit these dependencies.

So in some cases, there is no clear answer whether a given file should be ignored, and you have to choose between minimalism/tidiness and reproducibility/pragmatism. In scientific projects, the latter one probably wins more often then in software development, but there are still enough cases where ignoring is useful.

How can files be ignored?

An ignore file is a text file where each line consists of a filename pattern. If a file in your project matches one of these patterns, it is ignored. If you need patterns for the programming languages or tools used in your project, I recommend using , where you can generate appropriate patterns for your environment very quickly. Enter keywords like matlab or linux, hit Create, and copy the resulting text. These are some prevalent examples, but you can read the full syntax description in the gitignore docs.

# Ignore each file named example.txt
example.txt
# Ignore example.txt in the root directory
/example.txt
# Ignore example.txt in the public directory
/public/example.txt
# Ignore every file or directory named example
example
# Ignore every directory named example
example/
# Ignore the example directory in the root directory
/example/

There are three file locations where these patterns can be stored, depending on their intended usage scenario. The usage scenario is determined by the scope and privacy of the patterns:

Privacy
Should the patterns be shared with others or private?
Scope
Should the patterns be applied to a specific project or to every project on the machine?
Locations for ignore files depending on their usage scenario
Privacy
Scope Shared Private
Project .gitignore .git/info/exclude
Machine $HOME/.config/git/ignore

Project

You can share ignore patterns with your project collaborators by putting them in a file named .gitignore under project root and committing that file. This is useful for languages and tools clearly used by your project. If you have generated patterns for matlab, python, or something similar via , the .gitignore file is a good place for them. If you really need more customization, you can also put additional .gitignore files into project subdirectories. Patterns for auxiliary files generated by LaTeX could e.g. go into the dissemination subdirectory.

System

You might have some files that you always want to ignore in each project. These are mostly volatile auxiliary files which are generated by your personal working environment (OS thumbnails, favorite editor). It is considered good practice to put such patterns in a file in your user's home directory, normally $HOME/.config/git/ignore. Just generate a set of patterns for your OS and your preferred editors via and put them into that file. Your project's gitignore files will remain clear and focused (collaborators don't need to know your personal setup), and your machine is “vaccinated” against committing environment-specific files by mistake which don't contribute any value.

Repository

If you have some files that you only want to ignore in a concrete repository and the patterns should not be shared with others, the patterns can go into .git/info/exclude in the according repository. Patterns in this file are exclusively applied to the working directory of this repo and not shared with other clones. Such files could be some temporary test data or media that you don't want to integrate into the project.

Conclusion

Gitignore files are one of these tools that enable users to customize git for their projects and environments. It's no central part of version control, but it can remove distracting noise from the commit history and change list, and lets you concentrate on the relevant parts of your work. You can do yourself and others a great favor by setting up good gitignore files.