Git

How to Clear Git Local Cache

Git is a source code management tool for DevOps projects. It is considered to manage small to large projects. It enables programmers to work with other members in non-linear development. Git supports local repositories as well as remote repositories. The local repositories are accessible only on local machines, whereas the remote repositories are accessible remotely.

During testing, some committed files may cause a problem or harm the entire project or may slow down the project due to a large number of unnecessary files. For this purpose, Git users may choose to clean the cache of their local repository or delete certain files from the repository.

In this write-up, we will illustrate how to clear a Git local cache.

How to Clear Git Local Cache?

To clear the Git local cache, first, open the Git local repository. Then, commit the staged files. To remove these files or to clear the Git repository cache, utilize the “$ git rm –cached” command.

Take a look at the below-provided steps to clear the Git local cache.

Step 1: Open Git Bash Terminal
Launch the Git “Git Bash” terminal from the Start menu:

Step 2: Navigate to Git Local Repository
Next, navigate to the Git local working repository:

$ cd "C:\Git"

Step 3: Create New File
Create a new text file through the “touch” command:

$ touch File.txt

To verify whether the file is created or not, check out the Git repository status:

$ git status

It can be seen that the file is successfully added to the Git repository but has not been tracked yet:

Step 4: Add All Untracked Files to Staging Environment
To track the unstaged files, utilize the “git add” command with the “.” option:

$ git add .

Step 5: Commit Staging Files
Next, commit all staged files by utilizing the “git commit” command:

$ git commit -m "Committed changes"

Step 6: Delete Specific File From Git Cache
In order to remove the specific file from the Git cache, utilize the “git rm –cached” command and specify the name of the file you want to remove:

$ git rm --cached File.txt

Step 7: Clear Git Local Cache
To delete all files from the Git local cache, utilize the “git rm -r –cached .” command. Here, the “-r” option is used to remove files recursively from Git local cache:

$ git rm -r --cached

Step 8: Check Repository State
Lastly, check the Git local repository state using the “git status” command:

$ git status

Here, you can see we have successfully cleared the Git local cache:

Here you go! You have learned the procedure to clear a Git local cache.

Conclusion

To clear the Git local cache, first, open the Git working repository and add all untracked files to a staging environment. Next, commit all the staged files through the “$ git commit -m” command. Then, to remove a specific file from the local Git cache, utilize the “$ git rm –cached <filename>” command. However, to clear the whole Git local cache, use the “$ git rm -r –cached .” command. This tutorial has taught you how to clear Git’s local cache.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.