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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github