Home Software Engineering How To Clear Git Cache

How To Clear Git Cache

by schkn

When working with Git, you may want to add some new lines to your gitignore files.

However, when listing the files to be committed in your staging area, you realize that some of the ignored files are still showing up.

In this case, you may need to clear your Git cache.

In this tutorial, we are going to provide a comprehensive guide on how to clear your Git cache.

We are also going to explain in details how to actively remove files you wanted to ignore in the first place.

Ready?

Clear Git Cache using rm

Usually, you want to clear your Git cache because you added new entries in your gitignore files and you want them to be taken into account.

The easiest way to clear your Git cache is to use the “git rm” command with the “–cached” option.

You can choose to remove one file or to remove an entire working directory.

$ git rm --cached filename

Concrete example

Note : do not forget the cached option or your file will be deleted from the filesystem.

For this example, the .gitignore file is set to ignore all files ending in “.conf

Content of .gitignore:

*.conf

However, the file named “file.conf” is already in the staging area of my Git repository, this is also called the index.

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   file.conf

In this case, you want your file to go from the staging area back to the working directory (essentially the untracked part of Git).

To clear the cache, you use the git rm command.

When provided with the “–cached” option, it will only delete files from the staging area, not from the working directory.

$ git rm --cached file.conf

$ git status

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file.conf

nothing added to commit but untracked files present (use "git add" to track)

Clear Entire Git Cache

In some cases, you may want to clear the cache of your entire Git staging area.

This is particularly useful when you added multiple files that you want now to be ignored via your .gitignore file.

To clear your entire Git cache, use the “git rm” command with the “-r” option for recursive.

$ git rm -r --cached .

When all files are removed from the index, you can add the regular files back (the one you did not want to ignore)

$ git add .
$ git commit -am 'Removed files from the index (now ignored)'

Concrete Example

For this example, the .gitignore file is set to ignore files ending in .conf.

Content of .gitignore:

*.conf

In the staging area, we have two files ending in .conf and three regular files ending in .js.

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   file.conf
        new file:   file2.conf
        new file:   index.js
        new file:   script.js

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

        modified:   .gitignore

First, let’s remove all the files that are currently tracked.

$ git rm -r --cached .
rm '.gitignore'
rm 'README.md'
rm 'file.conf'
rm 'file2.conf'
rm 'index.js'
rm 'script.js'

Now some of the files may be marked as deleted and some others are back to the working directory.

Now, you want to add them back to the staging area while taking into account the content of your .gitignore file.

$ git add .

$ git status

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   index.js
        new file:   script.js

Awesome!

The configuration files are not in the staging area anymore.

You can now commit and push them to your repository.

$ git commit -am 'Removed files from the index (now ignored)'

$ git push

Note : do not forget to set your upstream branch when pushing your changes.

Conclusion

In this tutorial, you learnt how you can clear your Git cache easily and how it can help when you updated your .gitignore file.

You also learnt more about the “git rm” command and how it can be used in order to remove some files from the staging area back to the working directory.

If you are curious about Git or software engineering, we have a complete section dedicated to it on the website.

Make sure to check it out!

Clear Git Cache

You may also like

3 comments

Links 15/11/2019: New Opera and Brave, GNU/Linux Flatpa(c)ked | Techrights November 15, 2019 - 10:48 am

[…] How To Clear Git Cache […]

Reply
omid January 26, 2021 - 2:13 pm

Hi
I have a file that added to gitignore but that is showing in stage status
I try to use git rm –cached but not working

Reply
G April 19, 2021 - 8:39 am

Got- it

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.