How to Remove the File or Folder from Git Cache/Git Tracking

February 3, 2022
git
How to Remove the File or Folder from Git Cache/Git Tracking

In this tutorial, you will learn How to Remove the File or Folder from the Git Cache and Git Tracking.

Let me give you an example, lets’s say you are maintaining the git and you were uploading some files e.g env. Let’s say you have now another person working on the same project and both need to do local development before shipping the new features.

To avoid updating env again and again, we need to remove env files from the git but not from both developer systems.

So in order to do that we need to remove the file or folder from the Git Cache.

Let’s get started!

Syntax for removing the file from git cache and git tracking:

git rm --cached <file_name>

Here at <file_name> you need to type the file name itself, whereas rm stands for removing.

This will remove the file from the git cache and git will no longer able to track it.

After running the above command, make sure you are commit it as well.

git rm --cached <file_name>
git add .
git commit -m "<your_message>"
git push origin <branch_name>

Syntax for removing the folder and its files recursively from git cache and git tracking:

git rm -r --cached <folder_name>

Here at <folder_name> you need to type the folder name itself, whereas rm stands for removing and -r for recursive.

This will remove the folder and its files recursively from the git cache and git will no longer be able to track it.

After running the above command, make sure you are commit it as well.

git rm -r --cached <folder_name>
git add .
git commit -m "<your_message>"
git push origin <branch_name>

Conclusion:

Hope you have learned today how to remove the file or folder from the Git Cache and make git untrack that file 🙂

Let me know if you have any questions

I will see you in the next one.

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon