Are you worried about adding some new lines to your gitignore files while working with Git? And do you realize such files are still shown in git? Then, Git Clear Cache comes into the frame. Furthermore, you may also get to know some Git commands that help to clear a git repo’s cache from here.
In this tutorial, we are going to discuss how to clear git cache in different ways. By using the commands, you can easily clear the git cache. Moreover, we are also explaining thoroughly how to actively remove files you needed to ignore in the first place.
- Commands to Clear a Git Repository’s Cache
- Clear Git Cache using rm
- Concrete example
- Clear Entire Git Cache
- Concrete Example
- Clear specific files from Git cache
Commands to Clear a Git Repository’s Cache
Here are the commands for clearing out your git repo’s cache in order for the changes to take place.
git rm -r --cached . git add . git commit -am 'git cache cleared' git push
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
- How To Delete File on Git | Removing Files from Git Repository using git rm Command
- How To Unstage Files on Git | Different Ways to Unstage Files in Git
- How To Clean Up Git Branches | Git Clean Up Local and Remote Branches
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).
Also Check: How To Delete Local and Remote Tags on 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 ones 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 in 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.
Clear specific files from Git Cache
Let’s discuss how to clear the git cache for specific files here. After using the following commands, it will remove the specific file from git.
git rm --cached database.php git add --all git commit -m "Remove the database config file cache" git push origin branch_name
Ultimately, you have learned how to remove the cache for specific files using the git command. It will assist when you add a new file on the gitignore file and it’s not excluded from git.
Conclusion
In this tutorial, you learned how you can clear your Git cache easily and how it can help when you updated your .gitignore file.
You also learned 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!