Remove a File from a Git Repository without Deleting It from the Local Filesystem

Better Stack Team
Updated on June 24, 2024

To remove a file from a Git repository without deleting it from the local filesystem, you can use the git rm --cached command. Here's how you can do it:

 
git rm --cached <file>

Replace <file> with the name of the file you want to remove from the repository but keep in the local filesystem.

After running this command, the file will be removed from the Git repository's staging area and history, but it will remain in your local filesystem.

Note:

  • Be cautious when using git rm --cached, as it only removes the file from the repository and not from your filesystem. Make sure you don't accidentally delete files that you still need.
  • If you have already staged changes using git add, you'll need to unstage the file first using git reset HEAD <file> before running git rm --cached.
  • Remember to commit the changes after running git rm --cached to make the removal permanent in the repository.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.