How to Add File Entries to the gitignore File in Git
This tutorial will discuss adding file entries to the .gitignore
file in Git.
Add File Entries to the .gitignore
File in Git
Git tracks the modifications of all the files in the project directory of a Git repository. We may wish to inhibit specific files from tracking in a Git repository.
Typically, the files we wish to ignore from tracking are created as part of the build process, temporary files, or system-generated files.
Some of the common types of files typically ignored from tracking in a Git repository are the following:
- Code files created upon compilation such as
.o
,.pyc
, or.class
files; - Output directories for the build such as
/bin
,/out
, or/target
; - Files that are generated at run time, such as
.log
,.lock
, or.tmp
; - System files that are hidden such as
.DS_Store
orThumbs.db
; - Config files for the personal IDE such as
.idea/workspace.xml
; - Editor temp files such as
.swp
or.swo
(produced by Vim editor); - Package files or compressed files such as
.jar
,.war
,.nar
,.zip
,.tar
,.gz
or.rar
.
We can use the .gitignore
file feature of Git to ignore the files from tracking. It is a special file typically checked in at the root of the project directory in the Git repository.
There is no special command to trigger the ignore process. The .gitignore
file needs to be updated and committed to the repository whenever we have new files that we wish to ignore.
We need to add patterns in the .gitignore
files that are matched with the filenames in the Git repository to decide whether to add or ignore them.
Below are the globbing patterns used by the .gitignore
to match against the filenames.
.log
ignores log files with.log
extension in the directories, ex. debug.log, .log, logs/debug.log/bin
ignores thebin
folders.class
ignores the compiled class files.tmp
ignores thetmp
(temporary) fileslogs
ignores both files and the contents of directories with the namelogs
.
As per the convention, we can place the .gitignore
file in the top-level directory of our repository. We can also add multiple .gitignore
files in sub-directories.
Patterns in a particular .gitignore
file are tested relative to the directory containing that file.
See below an excerpt of a sample .gitignore
file.
$ cat .gitignore
# ignore the bin folders
**/bin/
# Compiled class file
*.class
# Log file
*.log
# tmp files
*.tmp
# Vim temp files
*.swp
*.swo
...
The #
at the start of the line adds comments in the file.
We can also add personal ignore patterns in our local system repository. We need to add them in a particular file at location git/info/exclude
in our local system.
This is not versioned and not committed & distributed with our repository.
We can also define global Git ignore patterns for all the repositories present across our local system. We need to set the Git global property core.excludesFile
.
Thus, for example, we can add the global .gitignore
file in our home directory and configure its location with the command git config
as follows.
$ touch ~/.gitignore
$ git config --global core.excludesFile ~/.gitignore
We have learned to add file entries to the .gitignore
file in Git.
For more information, please visit the following.