How to Remove a File from Git without Deleting it Locally?
Last Updated :
23 Jul, 2025
Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. There may be instances where we need to remove a file from the Git repository, but want to keep it in your local filesystem. This situation can arise for a configuration file.
Steps to Remove a File from a Git Repository without Deleting it From the Local Filesystem
Step 1: Check the current status of your repository
Suppose we’re working on a Git repository. Now we want to see all the files in the filesystem and the current status of the Git repository.
For Linux or MacOS:
ls && git status
For Windows:
dir && git status
OutputI have two C source files named "index.c" and "main.c". Both of these files have been committed and are currently being tracked by Git.
Step 2: Remove the File from the Git Repository
To remove a file from the Git repository without deleting it from your local filesystem, we'll need to use the git rm command with the --cached option.
Now let's try to remove index.c from the git repository
git rm --cached file-name.extension
Output:
OutputWe can see, the index.c file is “deleted“. Further, since its local copy is still there, it has been marked as “untracked”.
Step 3: Commit Changes
Now commit the changes
git commit -m "commit message"
Output:
OutputWe can see index.c is deleted from the git repository.
Step 4: Final Output
Now check the currently staged files.
git ls-files
and all files in the local filesystem respectively.
ls
Output:
OutputThe output shows the index.c file is not there anymore. But local copies are kept. We have removed the file from the git repository successfully and the file is now untracked but remember that the files removed by git rm -–cached still live in Git’s commit history.
Similar Reads
How to Remove Deleted Files from a Git Repo? Managing files in a Git repository often involves adding, modifying, and deleting files. However, sometimes files are deleted from the disk but remain tracked in the repository. To keep your repository clean and in sync with your working directory, you need to remove these files from the repository
3 min read
How to Delete a File From a Git Repository? Managing files within a Git repository often involves adding new files, modifying existing ones, and sometimes, deleting files that are no longer needed. Deleting a file from a Git repository is a simple process, but it's essential to follow the correct steps to ensure that the repository remains co
3 min read
How to Remove Local Untracked Files From Current Git Working Tree? When working on a Git repository, itâs common to create temporary files or directories that you no longer need. These untracked files can clutter your working tree and make it harder to focus on the changes that matter. Fortunately, Git provides simple commands to clean up these untracked files. Thi
5 min read
How to Remove Files From Git Staging Area? The Git staging area (also known as the index) is an intermediate space where Git stores changes that are about to be committed. When you add a file to the staging area using git add, it is prepared for the next commit. However, there are situations where you might need to remove a file from the sta
2 min read
How to Remove a Large File from Commit History in Git? Simply removing a large file through a commit won't truly eliminate it. Git stores all versions of files in its history, even deleted ones, to allow for easy recovery. This can lead to wasted disk space. We will discuss the various approaches for removing a large file from the commit history in Git:
4 min read
How to Delete a Remote Tag in Git? Git tags are important for marking specific points in your repository's history, often used for releases. However, there may come a time when you need to delete a remote tag. Hereâs a simple guide to help you understand how to do this. Table of Content What is a Git Tag?Why Delete a Remote Tag?Steps
2 min read