Git: Stop Tracking File After Adding to .gitignore

Git: Stop Tracking File After Adding to .gitignore

Accidentally forgot to add a file to the .gitignore file and started tracking it with other files? Or perhaps a remote repository already has a file tracked that you've cloned, but would like to stop tracking?

Just as important as tracking changes is not tracking them in certain cases, lest they change the scale of what you're keeping an eye on. Once a fille is tracked, even if you add it to .gitignore, it will still be tracked.

In this tutorial, we'll take a look at two ways on how to stop tracking a file on Git, after adding it to .gitignore.

Choosing between these two depends on whether you want the file to be kept in the repository or not.

Stop Tracking File and Remove It From the Repository

First, let's go ahead and create a couple of files, add them to the index and commit themm to the repository:

$ touch file.txt
$ touch file2.txt
$ git add .
$ git commit -m "Initial commit"

These two files are now tracked, and all changes made to them are relevant in Git's eyes. Though... we've forgot to add a .gitignore file, and file.txt we don't want to track is already in the repository.

Let's quickly create a .gitignore and add the matcher for file.txt in it:

$ touch .gitignore
$ echo "file.txt" >> .gitignore
$ git add .gitignore
$ git commit -m "Adding .gitignore"

When we make a new change to the file:

$ echo "new line" >> file.txt
$ git status

Even though it's now present in the .gitignore file - the file is still being tracked:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   file.txt

We'd like to keep file.txt present on our local system (it "includes important configurations" but since the configurations are machine-specific, it's only applicable to us). To make Git stop trackng a file, while removing it from the Git repository, we'll run the git rm command:

$ git rm --cached file.txt
rm 'file.txt'

Note: The --cached flag removes only the files from the index - the working directory isn't affected at all.

Let's verify that the Git repository no longer lists this file, while it is still present in the working tree:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

$ git ls-files   
file2.txt         
.gitignore

$ ls -a
.		.git		file.txt
..		.gitignore	file2.txt

Stop Tracking File Without Removing It From the Repository

Alternatively, you might want to have the file still present in the repository, for other team members to be able to download - but it's a hassle for you personally to keep tracking changes to it, you can stop tracking it while keeping it in the repository via the git update-index command:

$ git update-index --skip-worktree file.txt
$ git status
On branch main
nothing to commit, working tree clean

This command updates the index and skips over alll the files specified. In our case, it updates the index to skip over file.txt and turn tracking off. In that sense - this command can be used individually, separately of the .gitignore file.

Note: An alternative is the git update-index --assume-unchanged command, in which you tell Git to trust you on your word that the file is unchanged. This is done for huge files that are not supposed to be changed, and are expensive to check for changes constantly. If you use this approach - don't change the file, lest you'll break the feature's intended use and Git will encounter errors while merging. Using --skip-worktree is preferred.

Let's verify that the file is present both in the repository and our local file system:

$ git ls-files
.gitignore
file.txt
file2.txt

$ ls -a
.		.git		file.txt
..		.gitignore	file2.txt
Last Updated: June 25th, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Better understand your data with visualizations

    With over 330+ pages, you'll learn the ins and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh, and more.

    © 2013-2021 Stack Abuse. All rights reserved.