Skip to content

How to ignore certain files

Your repository may contain files that you don't want to include in your commit history. For example, you may not want to include files of the following types:

  • Sensitive data files for which access must be strictly controlled.
  • Temporary files that do not contain useful information, such as:
  • .aux files, which are generated when compiling LaTeX documents; and
  • .pyc files, which are generated when running Python code.
  • Files that can be automatically generated from your commit history, such as:
  • .pdf versions of LaTeX documents; and
  • documentation generated from your code files.

You can instruct Git to ignore certain files by creating a .gitignore file. This is a plain text file, where each line defines a pattern that identifies files and directories which should be ignored. You can also add comments, which must start with a #, to explain the purpose of these patterns.

Tip

If your editor will not accept .gitignore as a file name, you can create a .gitignore file in your repository by running:

touch .gitignore

For example, the following .gitignore file would make Git ignore all .aux and .pyc files, and the file my-paper.pdf:

# Ignore all .aux files generated by LaTeX.
*.aux
# Ignore all byte-code files generated by Python.
*.pyc
# Ignore the PDF version of my paper.
my-paper.pdf

If you have sensitive data files, one option is to store them all in a dedicated directory and add this directory to your .gitignore file:

# Ignore all data files in the "sensitive-data" directory.
sensitive-data

Tip

You can force Git to add an ignored file to a commit by running:

git add --force my-paper.pdf

But it would generally be better to update your .gitignore file so that it stops ignoring these files.