Git allows you to specify which files should be omitted from revision control.
You can specify which files Git should ignore from revision control. While you can always omit files from revision control simply by not staging them, having an 'ignore-list' is more convenient, especially if there are files inside the working folder that are not suitable for revision control (e.g., temporary log files) or files you want to prevent from accidentally including in a commit (files containing confidential information).
A repo-specific ignore-list of files can be specified in a .gitignore
file, stored in the root of the repo folder.
The .gitignore
file itself can be either revision controlled or ignored.
- To version control it (the more common choice – which allows you to track how the
.gitignore
file changes over time), simply commit it as you would commit any other file. - To ignore it, simply add its name to the
.gitignore
file itself.
The .gitignore
file supports file patterns e.g., adding temp/*.tmp
to the .gitignore
file prevents Git from tracking any .tmp
files in the temp
directory.
SIDEBAR: .gitignore
File Syntax
Blank lines: Ignored and can be used for spacing.
Comments: Begin with
#
(lines starting with # are ignored).# This is a comment
Write the name or pattern of files/directories to ignore.
log.txt # Ignores a file named log.txt
Wildcards:
*
matches any number of characters, except/
(i.e., for matching a string within a single directory level):abc/*.tmp # Ignores all .tmp files in abc directory
**
matches any number of characters (including/
)**/foo.tmp # Ignores all foo.tmp files in any directory
?
matches a single characterconfig?.yml # Ignores config1.yml, configA.yml, etc.
[abc]
matches a single character (a, b, or c)file[123].txt # Ignores file1.txt, file2.txt, file3.txt
Directories:
- Add a trailing
/
to match directories.logs/ # Ignores the logs directory
- Patterns without
/
match files/folders recursively.*.bak # Ignores all .bak files anywhere
- Patterns with
/
are relative to the.gitignore
location./secret.txt # Only ignores secret.txt in the root directory
- Add a trailing
Negation: Use
!
at the start of a line to not ignore something.*.log # Ignores all .log files !important.log # Except important.log
Example:
# Ignore all log files
*.log
# Ignore node_modules folder
node_modules/
# Don’t ignore main.log
!main.log
1 Add a file into your repo's working folder that you presumably do not want to revision-control e.g., a file named temp.txt
. Observe how Git has detected the new file.
Add a few other files with .tmp
extension.
2 Configure Git to ignore those files:
Create a file named .gitignore
in the working directory root and add the text temp.txt
into it.
echo "temp.txt" >> .gitignore
temp.txt
Observe how temp.txt
is no longer detected as 'untracked' by running the git status
command (but now it will detect the .gitignore
file as 'untracked'.
Update the .gitignore
file as follows:
temp.txt
*.tmp
Observe how .tmp
files are no longer detected as 'untracked' by running the git status
command.
The file should be currently listed under Unstaged files
. Right-click it and choose Ignore...
. Choose Ignore exact filename(s)
and click OK
.
Also take note of other options available e.g., Ignore all files with this extension
etc. They may be useful in future.

Note how the temp.text
is no longer listed under Unstaged files
. Observe that a file named .gitignore
has been created in the working directory root and has the following line in it. This new file is now listed under Unstaged files
.
temp.txt
Right-click on any of the .tmp
files you added, and choose Ignore...
as you did previously. This time, choose the option Ignore files with this extension
.
Note how .temp
files are no longer shown as unstaged files, and the .gitignore
file has been updated as given below:
temp.txt
*.tmp
3 Optionally, stage and commit the .gitignore
file.
done!
Files recommended to be omitted from version control
- Binary files generated when building your project e.g.,
*.class
,*.jar
,*.exe
Reasons:- no need to version control these files as they can be generated again from the source code
- Revision control systems are optimized for tracking text-based files, not binary files.
- Temporary files e.g., log files generated while testing the product
- Local files i.e., files specific to your own computer e.g., local settings of your IDE (
.idea/
) - Sensitive content i.e., files containing sensitive/personal information e.g., credential files, personal identification data (especially if there is a possibility of those files getting leaked via the revision control system).