Git reset vs git rm — cached | Remove & Delete Added Files

Git reset vs git rm — cached | Remove & Delete Added Files

Git reset and git rm, are two different commands. Many get confused as both commands do a similar job, but they are not entirely the same. I’m explaining here both.

git reset vs git rm

Let’s understand the use of these two commands. You will also understand the difference between git reset and git rm.

Git Reset to Remove Added File

When you run

git add <file>

The given file along with the changes will be added to the staging area.

If you want to remove any file from the staging, you can run the git reset command.

git reset <file>

This will remove the file from the staging area but it will be available in the working directory.

This command is nothing but the undo command for git add.

This is one of the very basic and useful git commands, you will handy for your development.

Getting confused about the staging area and working directory?

The working directory is nothing but a copy of your repository on your local computer. When you execute the git add command, it will move the file from the working directory to the staging area. The staging area is an intermediate stage between your local and remote repository.

If you look at the below git lifecycle diagram, you can visualize different stages.

git command lifecycle

Git RM Command to Delete the File

With the previous git reset command, you remove the file from staging and it will be present in the working directory.

Now if you want to delete a file permanently from your stagging as well as from your working directory, use the below command.

git rm <file>

If you just want to undo the git add, use the –cache option. It simply unstage the file changes (remove the file from the staging area) and keeps it in the working directory.

git rm --cached <rm>

It is recommended to use these commands carefully, just to make sure, you don’t miss any code changes.

Conclusion

Hope you understand the difference between git reset and git rm command.

You might be wondering, why there are two different commands to do the same job. But it also gives us the flexibility to choose which you find more comfortable.

But sometimes, these multiple options also lead to confusion. And sometimes it results into the accidental deletion of the files.

Also, git revert is an entirely new command. Don’t get confused.

Learn about any git command before you execute.

Have fun coding!

Leave a Reply

Your email address will not be published. Required fields are marked *