How to Clear Git Cache

A photograph of a computer monitor showing a .gitignore file.

As you work with Git, you will have many instances where, after adding new lines of code to your .gitignore file, the ignored files still show up in your “git commit” staging area. When you are facing such issues, the best way to resolve the issue is to clear and clean your Git cache.

This guide shows you how to clear your entire Git cache. Further, it will also show how to remove any cached files, directories and credentials from your Git repository.

Clearing the Entire Git Cache

One of the easiest ways to fix your .gitignore file is to fully reset your Git cache directory. This will remove any old metadata on your current repository and Git will be able to properly apply your ignore list.

To start, open a terminal session and navigate to your Git repository’s folder:

cd ~/your-git-repository

Clear the entire repository cache by running git rm along with its recursive flag:

git rm -r --cached .

Check whether your repository has properly removed any old metadata for your repository and that it is ready for a commit:

git reset .
git status

Re-add any unmerged data from your repository by running the following:

git add .

Confirm your cache reset by creating a new commit on your repository:

git commit -am 'Reset the entire repository cache.'
A terminal showing a fully reset Git cache.

Clearing a Specific File or Directory From the Git Cache

Aside from resetting your entire Git cache you can also the git rm subcommand to remove individual files and directories. This can be useful if don’t want to wipe your current staging area but still want to remove a problematic file from your repository.

Navigate to your Git repository using the cd command:

cd ~/your-git-repository

Run the following command to remove an individual file from your staging area:

git rm --cached your-file-here.txt

You can also remove an entire directory tree from your staging area by adding the -r flag on git rm:

git rm -r --cached ./your/directory/here

Check whether Git has removed the unnecessary files and folders by running the following command:

git status
A terminal showing the process of removing a directory from the active stash.

Commit your changes to the Git tree to apply your new setting:

git commit -am 'Removed unnecessary files from the repository.'

Confirm that you have properly removed your file by checking the status of the repository’s index:

file ./.git/index
A terminal showing the current status of the repository's index file.

Clearing Cached Credentials From Git

Another brilliant feature of Git is its ability to hold authentication information when connecting to remote hosts. However, this can be a problem especially if you are using Git from a shared computer.

To remove any cached credentials, go to your target Git repository:

cd ~/your-git-repository

Run the following command to disable Git’s ability to accept any credential information for the current repository:

git config --local --unset credential.helper

Next, remove all the active credentials on the current session:

git credential-cache exit

Lastly, delete the default “credentials” file for your Git installation:

rm ~/.git-credentials

Removing unnecessary files and credentials in Git is just the first step in managing your project’s repository. Learn how you can be more efficient with Git by using Git aliases and Git hooks.

Image credit: Gabriel Heinzer via Unsplash. All alterations and screenshots by Ramces Red.

Is this post useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.