The git rm command removes a file from a Git repository. This command removes a file from your file system and then removes it from the list of files tracked by a Git repository. The –cached flag lets you delete a file from a Git repository without deleting it on your file system.
How do you remove a file from a Git repository? That’s a good question. It’s not always the case that you want a file to be part of a repository forever. You may decide that a file is no longer necessary to a project, and therefore you’ll want to remove it from the Git repository.
To remove a file from a Git repository, you can use the git rm command. It’s the opposite of the git add command; it removes files.
In this guide, we’re going to discuss how to use the git rm command. We’ll walk through an example of the git rm command to show you how it works. Let’s begin!
How Files Are Managed in Git
Before we discuss how to use the git rm command, we need to understand how files are managed in the Git version control system.
Files inside a Git repository can either be tracked or untracked.
Tracked files are files which have been included in a git commit. Untracked files are files which have not yet been committed to the repository. Typically, untracked files are those that you have created before creating a commit.
To add a file to a Git commit, you can use the git add command. This will make it a tracked file. If you want to stop a file from being tracked, you can use the git rm command.
How to Use the git rm Command
The git rm command removes a file or group of files from a Git repository. A file is removed from both your machine and the Git repository. To preserve the file on your local machine, use the –cached flag.
Without any flags, this command will remove a file from both a Git repository and your local working directory. This means that it deletes a file, just like the Linux rm command deletes a file from a computer.
The simplest use of this command is to remove a file. Let’s remove a file called settings.json from a Git repository:
git rm settings.json
This command removes settings.json from the tracking area of our repository. You can use this same command to remove multiple files. To do so, separate the names of the files or folders you want to remove with a space:
git rm settings.json .env
This will remove the settings.json and .env files from a repository.
By default, Git will conduct a safety check when you run the rm command to unstage and remove paths. This will make sure that the files on your current branch are the same as those in the staging index. You can override this behavior by using the -f, or –force, flag:
git rm -f settings.json
You should only use the -f flag when you are absolutely sure that you want to remove a file. Otherwise, a merge conflict may occur later down the line. If this does happen, you can use our guide on how to resolve a merge conflict to help you out.
Git rm –cached
The Git rm –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will be removed from the index tracking your Git project.
Let’s remove the settings.json working tree file from our repository but keep it in our project directory:
git rm --cached settings.json
When we push our next commit, the settings.json file will be removed.
As long as a file exists in your local working directory, you can add it back to your Git repository. You can do so using the git add command. Read more about git add in our tutorial on the git add command.
How to Undo a git rm Command
The git rm command only updates the staging area and the working directory until a commit has been made. This means you can revert the command. To undo a git rm command, you can reset your repository to the last commit using git reset:
git reset HEAD
The Git HEAD commit represents the last commit in our repository. Thus, this command reverts our repository to the last commit.
You can revert a git rm command even if you have already committed the changes to the repository. To do so, you can use the git reset command and specify the hash of the commit to which you want to revert:
git reset a7e3cce9637c74281e6590003b39d3990bbb2731
We are reverting a git repository to its previous commit. The string of letters after the word “reset” is the hash for the last commit.
Once we revert the repository, we can view how that repository appeared before the commit. This means that our file will still be staged. In other words, our git rm command will have been undone.
Git rm vs. rm
The git rm command removes a file from both your working machine and a Git repository. The rm command, on the other hand, does not remove a file from a Git repository.
If you want to remove a file from your Git repository, you must use git rm. This is because the git rm command executes instructions to remove a file from a Git repository. Not all files on a Linux system are in a Git repository, so the Linux rm command does not remove files from a Git repository.
The git rm command allows you to remove a file from a Git repository and your working directory. If you only want to remove a file from your Git repository, you can use the –cached flag.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Now you’re ready to use the git rm command like a command line expert!
To learn more about working with Git, read our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.