top of page
Writer's pictureSiddhesh Kadam

Git Ignore

We can specify a list of untracked files or folders to ignore in Git. There are some files or folders in every project that we do not want Git to track.

In this case, we can use the Git Ignore concept to define a list of files or folders that should be ignored.

.gitignore is a plain text file that should be present in the root directory of your project (where .git gets created) So let's understand how it works.


  • Create .gitignore File & Ignore Files

To understand this concept let's practically create a scenario. We'll create an app.log & cache. temp file in our project root directory.

[root@siddhesh MyProject1]# echo "app1 log of web server" > app.log 
[root@siddhesh MyProject1]# echo "db cache log event 1" > cache.temp
[root@siddhesh MyProject1]# ll
total 24
-rw-r--r--. 1 root root 23 Feb  7 09:45 app.log
-rw-r--r--. 1 root root 21 Feb  7 09:46 cache.temp
-rw-r--r--. 1 root root 11 Feb  2 13:06 myscript2.py
-rw-r--r--. 1 root root 10 Feb  2 13:06 myscript3.py
-rw-r--r--. 1 root root 10 Feb  7 09:45 myscript4.py
-rw-r--r--. 1 root root 10 Feb  2 13:06 myscript.py
[root@siddhesh MyProject1]#

Now, check the status of git using "git status"

[root@siddhesh MyProject1]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	app.log
#	cache.temp
nothing added to commit but untracked files present (use "git add" to track)
[root@siddhesh MyProject1]#

We can see in the output above that app.log & cache.temp is now showing as an untracked file because these are new files and staging doesn't have any information on these files.


To exclude these files from git tracking, we'll create a .gitignore file.

[root@siddhesh MyProject1]# cat .gitignore 
app.log
cache.temp
[root@siddhesh MyProject1]# 

Run "git status" once more to see if there has been any change.

[root@siddhesh MyProject1]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	.gitignore
nothing added to commit but untracked files present (use "git add" to track)
[root@siddhesh MyProject1]# 

App.log and cache.temp are no longer visible in the untracked file list, but a newly created.gitignore file is now listed as an untracked file. This indicates that git has started excluding the files app.log and cache.temp from its tracking. Let's go ahead and add .gitignore file in staging and do a commit.

[root@siddhesh MyProject1]# git add .gitignore 
[root@siddhesh MyProject1]# git commit -m "added .gitignore file to exclude unwanted files"
[master b3aaee0] added .gitignore file to exclude unwanted files
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore
[root@siddhesh MyProject1]# git status
# On branch master
nothing to commit, working directory clean
[root@siddhesh MyProject1]# 

Now our git status is showing clean without adding unwanted files in tracking.


Exception in .gitignore In some cases, you may want to add an exception in the .gitignore file stating that Git should ignore all files ending in .log and only track production.log. In this case, add the following line to your .gitignore file. An exclamation (!) mark followed by the file name will not get included in .gitignore file.

[root@siddhesh MyProject1]# cat .gitignore 
*.log
!production.log
cache.temp
[root@siddhesh MyProject1]# 

Run "git status" again to re-verify this.

[root@siddhesh MyProject1]# git status 
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   .gitignore
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	production.log
no changes added to commit (use "git add" and/or "git commit -a")
[root@siddhesh MyProject1]# 

Because of an exception added to.gitignore, "git status" now shows production.log as an Untracked file. Let's stage these changes and commit them.

[root@siddhesh MyProject1]# git add .
[root@siddhesh MyProject1]# git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   .gitignore
#	new file:   production.log
#
[root@siddhesh MyProject1]# git commit -m "added production.log to track production  log"
[master b9b016e] added production.log to track production  log
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 production.log
[root@siddhesh MyProject1]# 

  • Ignore a Previously Committed File in Git

In this section, we'll learn how to ignore already committed files in Git. For this, we'll now ignore myscript4.py which is already committed.

So first let's add myscript4.py in .gitignore and remove myscript4.py cache from the Git repository but not the actual file.

[root@siddhesh MyProject1]# echo "myscript4.py" >> .gitignore 
[root@siddhesh MyProject1]# git rm --cached myscript4.py
rm 'myscript4.py'
[root@siddhesh MyProject1]# git add .
[root@siddhesh MyProject1]# git commit -m "added myscript4.py in .gitignore"
[master acdbc46] added myscript4.py in .gitignore
 2 files changed, 1 insertion(+), 1 deletion(-)
 delete mode 100644 myscript4.py
[root@siddhesh MyProject1]# ls myscript4.py 
myscript4.py
[root@siddhesh MyProject1]# 

In the above output, we used "git rm —cached <filename>" to remove the file's cache from the Git repository. This is mandatory to add an already committed file in Git for .gitignore. "git rm --cached <filename>" does not remove the original file.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page