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:
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:
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' 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. dir/otherdir/file.ext otherfile.ext # Ignoring directories # Both the directory itself and its contents will be ignored. bin/ gen/ # Glob pattern can also be used here to ignore paths with certain characters. # For example, the below rule will match both build/ and Build/ [bB]uild/ # 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 bin gen # Ignoring files by extension # All files with these extensions will be ignored in # this directory and all its sub-directories. *.apk *.class # 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. java/*.apk gen/*.class # To ignore files only at the top level directory, but not in its # subdirectories, prefix the rule with a `/` /*.apk /*.class # 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 **/DirectoryA/ # 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 DirectoryA/**/DirectoryB/ # 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: !.gitignore # Use the backslash as escape character to ignore files with a hash (#) # (supported since 22.214.171.124) \#*#
.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.
.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:
.git/info/excludefile (using the same syntax as
.gitignore). The rules will be global in the scope of the repository;
Furthermore, you can ignore local changes to tracked files without changing the global git configuration with:
git update-index --skip-worktree [<file>...]: for minor local modifications
git update-index --assume-unchanged [<file>...]: for production ready, non-changing files upstream
See more details on differences between the latter flags and the
git update-index documentation for further options.
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
-X (caps) cleans up only ignored files. Use
-x (no caps) to also remove untracked files.
git clean documentation for more details.
See the Git manual for more details.