Difference Between Git RM --Cached and Git Reset File

John Wachira Aug 09, 2022
Difference Between Git RM --Cached and Git Reset File

This article discusses the difference between the git rm --cached and the git reset<file> commands. We will discuss the function of each command in a fit to catch the differences between the two.

Difference Between git rm --cached and git reset<file>

For easier context, we will explore what each command can accomplish and then analyze the different outputs the two commands display. Let’s jump right in.

the git rm --cached Command

At times, Git can give you the suggestion below after running the git status command.

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   .env

Let us try to unstage the .env file using the rm option. We will run the following:

$ git rm --cached .env

Let us run the git status command to check out the state of our working directory.

$ git status

Output:

git rm &ndash;cached image

From the output above, we can see that our .env file was deleted but remains under untracked files. Let’s see what the git reset<file> command will do.

the git reset <file> Command

We will run the git reset command as shown below:

$ git reset HEAD .env

We can now run the git status command to checkout our working tree.

$ git status

Output:

git reset image

From the output above, we can see that the git reset <file> command has done away with the modifications done to the file.

The git rm --cached command stages a file for removal but keeps the file untracked. On the other hand, the git reset <file> command will only unstage a file.

The git rm --cached option is ideal when you want to unstage an untracked file without modifying the working tree. The counterpart command, git reset <file> will reset the index and not the working tree.

In a nutshell, the git rm --cached command will remove paths in the index and leave them untracked, while the git rest<file> command will reset only the index. Use the git rm --cached command with untracked files.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git rm

Related Article - Git Reset