Skip to main content

Git Ignore file

Published on December 24, 2020

This document was migrated from DigiDocs

This section covers how to tell Git to ignore certain files and directories by using .gitignore files or the git update-index commands.

tip

Basic knowledge of Git is assumed in this tutorial. If you are new to Git, visit http://rogerdudler.github.io/git-guide/ to learn the basics first.

If you want a certain file or directory to be ignored for the long-term, you should choose to do so using .gitignore files. Otherwise, if you only want Git to ignore a certain file temporarily, before committing it at a later time, you should do so using the git update-index commands.

Using .gitignore files

Git uses .gitignore files to decide which files and directories to ignore before you commit. Files and directories specified in .gitignore will not be tracked nor staged when git add * or git commit -a commands are used.

To ignore a file or directory using .gitignore:

  1. Create a .gitignore file in the desired location (e.g. the project's root directory).
    • The .gitignore file affects all files and sub-directories in its directory.
      • It recursively affects all files and directories in its sub-directories.
      • Sub-directories with their own .gitignore file use their own .gitignore instead.
  2. Open the file and enter the filename or directory to be ignored (e.g. secret.txt). See example below.
  3. Save the file and commit it.
    • Commit it to share the ignore rules with other users using the same repository and also to version-control it.

Example .gitignore file:

# Ignore specific file
plaintext_password.txt

# Ignore specific file type using wildcards
*.html

# Ignore specific directory
site/
caution

If a file is already being tracked, adding it to .gitignore will not stop Git from tracking it. You will need to remove the file from the Git cache using the git rm --cached <file> command. After doing this, the file will no longer be tracked provided it is specified in .gitignore.

Using 'git update-index'

If you want Git to temporarily ignore a file which you will commit at a later time, you can do so easily by using the git update-index commands to tell Git to stop and start tracking the file.

To temporarily ignore (stop tracking) a file:

git update-index --assume-unchanged <file>

To start tracking a file again:

git update-index --no-assume-unchanged <file>
tip

Read the man page to find out more about git update-index rules.

Resources