Ignoring files and folders

suggest change

This chapter illustrates how to avoid adding unwanted files (or file changes) in a Git repo. There are several ways (global or local .gitignore, .git/exclude, git update-index --assume-unchanged, and git update-index --skip-tree), but keep in mind Git is managing content, which means: ignoring actually ignores a folder content (i.e. files). An empty folder would be ignored by default, since it cannot be added anyway.

Ignoring files and directories with a .gitignore file

You can make Git ignore certain files and directories — that is, exclude them from being tracked by Git — by creating one or more .gitignore files in your repository.

In software projects, .gitignore typically contains a listing of files and/or directories that are generated during the build process or at runtime. Entries in the .gitignore file may include names or paths pointing to:

  1. temporary resources e.g. caches, log files, compiled code, etc.
  2. local configuration files that should not be shared with other developers
  3. files containing secret information, such as login passwords, keys and credentials

When created in the top level directory, the rules will apply recursively to all files and sub-directories throughout the entire repository. When created in a sub-directory, the rules will apply to that specific directory and its sub-directories.

When a file or directory is ignored, it will not be:

  1. tracked by Git
  2. reported by commands such as git status or git diff
  3. staged with commands such as git add -A

In the unusual case that you need to ignore tracked files, special care should be taken. See: Ignore files that have already been committed to a Git repository.


Here are some generic examples of rules in a .gitignore file, based on glob file patterns:

# Lines starting with `#` are comments.

# Ignore files called 'file.ext'

# Comments can't be on the same line as rules!
# The following line ignores files called 'file.ext # not a comment'
file.ext # not a comment 

# Ignoring files with full path.
# This matches files in the root directory and subdirectories too.
# i.e. otherfile.ext will be ignored anywhere on the tree.

# Ignoring directories
# Both the directory itself and its contents will be ignored.

# Glob pattern can also be used here to ignore paths with certain characters.
# For example, the below rule will match both build/ and Build/

# Without the trailing slash, the rule will match a file and/or
# a directory, so the following would ignore both a file named `gen`
# and a directory named `gen`, as well as any contents of that directory

# Ignoring files by extension
# All files with these extensions will be ignored in
# this directory and all its sub-directories.

# It's possible to combine both forms to ignore files with certain
# extensions in certain directories. The following rules would be
# redundant with generic rules defined above.

# To ignore files only at the top level directory, but not in its
# subdirectories, prefix the rule with a `/`

# To ignore any directories named DirectoryA 
# in any depth use ** before DirectoryA
# Do not forget the last /, 
# Otherwise it will ignore all files named DirectoryA, rather than directories
# This would ignore 
# DirectoryA/
# DirectoryB/DirectoryA/ 
# DirectoryC/DirectoryB/DirectoryA/
# It would not ignore a file named DirectoryA, at any level

# To ignore any directory named DirectoryB within a 
# directory named DirectoryA with any number of 
# directories in between, use ** between the directories
# This would ignore 
# DirectoryA/DirectoryB/ 
# DirectoryA/DirectoryQ/DirectoryB/ 
# DirectoryA/DirectoryQ/DirectoryW/DirectoryB/

# To ignore a set of files, wildcards can be used, as can be seen above.
# A sole '*' will ignore everything in your folder, including your .gitignore file.
# To exclude specific files when using wildcards, negate them.
# So they are excluded from the ignore list:

# Use the backslash as escape character to ignore files with a hash (#)
# (supported since

Most .gitignore files are standard across various languages, so to get started, here is set of sample .gitignore files listed by language from which to clone or copy/modify into your project. Alternatively, for a fresh project you may consider auto-generating a starter file using an online tool.

Other forms of .gitignore

.gitignore files are intended to be committed as part of the repository. If you want to ignore certain files without committing the ignore rules, here are some options:

Furthermore, you can ignore local changes to tracked files without changing the global git configuration with:

See more details on differences between the latter flags and the git update-index documentation for further options.

Cleaning up ignored files

You can use git clean -X to cleanup ignored files:

git clean -Xn #display a list of ignored files
git clean -Xf #remove the previously displayed files

Note: -X (caps) cleans up only ignored files. Use -x (no caps) to also remove untracked files.

See the git clean documentation for more details.

See the Git manual for more details.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents