How to Make Git Stop Tracking a File Without Deleting it

Git is a very popular and powerful distributed version control system used by developers all across the globe. Just as it is important to track changes to files in Git, in certain cases, it can be equally important not to track them so they would not change the scale of the processes you wish to keep an eye on. This is why in today’s article, our web development agency Chicago explores ways to stop tracking a file in Git. The approach you choose to take depends on whether you wish to keep the file in the Git repository or not.

What is a Git repository?

Git repository is a storage for the files in your project that allows you to save as well as have access to different versions of your code. You can create a new git repository with the git init command or make a clone of the existing repository by running the git clone command.

What is the git rm command?

This command removes a particular file or a group of files from a git repository. It can also be used to remove files from the working directory and the staging index. However, you can not use it to remove a file only from the working directory without affecting the staging index.

What is a .gitignore file?

Git classifies all the files of the working copy in three ways:

  • A tracked file: The file that has already been committed or staged.
  • An untracked file: The file that has not previously been committed or staged.
  • An ignored file: The file that has been commanded to be ignored.

As you can see, Git labels all the files the three ways we mentioned above. Those files that you told Git to ignore will be stashed in the file called .gitignore. To commit these files, you’ll first need to derive them from the Git repository. The .gitignore will prevent files that Git does not track from being added to the set of files that the version control system is tracking. On the other hand, Git will not stop tracking a file that is already being tracked, even if you add it to .gitignore.

How to tell Git to stop tracking a file and remove it from the repository

We’ll start by creating a couple of files. After that, we’ll add them to the index and commit them to the repository. Here is how to do that:

touch file1.txt

touch file2.txt

git add .

git commit -m “Initial commit”

Git will now track all the changes made to these two files. Now, let’s say we want Git to stop tracking the file we named file1.txt. Since it is already in the repository, we need to create a .gitignore file and add the matcher for file1.txt to it. To do so, simply follow this command line:

touch .gitignore

echo “file1.txt” >> .gitignore

git add .gitignore

git commit -m “Adding .gitignore”

Now, let’s make some changes to file1.txt:

echo “new line” >> file1.txt

git status

Although file1.txt is included in the .gitignore file, Git is still tracking it:

Changes not staged for commit:

     (use “git add <file>…” to update what will be committed)

     (use “git restore <file>…” to discard changes in the working directory)

          modified: file1.txt

But, file1.txt contains some important configurations, so we wish to keep it in the working directory of our local machine while removing it from the repository. To tell Git to stop tracking a file and remove it from the Git repository, we’ll use the git rm command:

git rm –cached file1.txt

rm ‘file1.txt’

Notice that we used the –cached flag here. This tells Git to remove files only from the index. This way our working directory won’t be affected.

The only thing left to do is to verify that file1.txt is no longer in the Git repository while it is still present in our working tree:

git ls-files

file2.txt

.gitignore

ls -a

.          .git               file1.txt

..         .gitignore     file2.txt

How to tell Git to stop tracking a file without removing it from the repository

You may sometimes find it necessary to keep the file you no longer need to track in the Git repository so other members of your team can download it. This can easily be achieved with the git update-index command:

git update-index –skip-worktree file1.txt

git status

On branch main

noting to commit, working tree clean

This way, you told Git to update the index while skipping over the specified files. When it comes to our example, we have skipped over file1.txt and turned the tracking off. This command can also be used separately from the .gitignore file.

You can also achieve the same result by using the git update-index –assume-unchanged command. However, you need to proceed with caution here! This approach is used only for large files that are supposed to remain unchanged. Otherwise, you will break the feature’s intended use, and Git will encounter errors while attempting the merger. This is why we recommend using the –skip-worktree command.

Now, all we need to do is check if the file is still present in the Git repository as well as in our local file system. Let’s see:

git ls-files

.gitignore

file1.txt

file2.txt

ls -a

.          .git               file1.txt

..         .gitignore     file2.txt

How to stop tracking an entire folder

You may also need to remove an entire folder from the index. In order to do so, you’ll first need to add the folder name to .gitignore and then run the commands:

git rm –cached -r <folder>

git commit -m “<Message>”

Notice that we’ve added the -r component to the command line. Omitting it would fail the process, so that Git would display the following message:

fatal: not removing ‘folder’ recursively without -r.

How to stop tracking multiple ignored files

Next, let’s explore how to remove all files that are currently in .gitignore from the index:

git rm -r –cached .

git add .

git commit -m “Removes all .gitignore files and folders”

The first line tells Git to remove all the files from the index. The second line re-adds all the files to the index except those in .gitignore, while the third line commits the change.

Final thoughts

Git is an extremely powerful web development tool. However, due to its steep learning curve, we’ve witnessed many companies struggle with their projects and fail to meet deadlines. We hope that this article helped you clarify any possible ambiguities on how to stop Git from tracking files without actually deleting them from your machine. In case you need any further help with your project, or if you simply wish to expand your company’s reach with our custom-coded websites, schedule a call with our seasoned developers.

Brian Djordjevic
About The Author

Brian Dordevic

Brian is Marketing Strategic Planner with a passion for all things digital. Feel free to follow him on Twitter or schedule a consultation call with him.

Want to start your creative project today? Fill out this form, and let’s discuss your next steps.

Tell Us About Your Project.

Yes, I'm interested in discussing a

project for

. My name is


and you can email me