How to Undo the Git add Command

John Wachira Feb 15, 2024
How to Undo the Git add Command

This article discusses removing a file you have added to the index for commit. There are two commands you can use when you want to remove a file from your index.

Let’s explore each of them with a practical example.

Undo the Git add Command

To simulate a situation where we mistakenly add a file for commit, we will edit a file in our repository and use the git add command to stage the file for commit.

$ git status

git status after git add

Now that our file is on our index, how do we unstage it?

Your first option is Git’s suggestion. You can use the git restore --staged<file> command, as shown below.

$ git restore --staged Load.txt

Let’s check our index.

$ git status

git status after git restore

We have successfully unstaged the Load.txt file from our index with the git restore --staged<file> command. What happens when you omit --staged?

From the git restore documentation, we can conclude that running git restore Load.txt will restore our working tree and discard the changes in the staged file.

Let’s check out the second method. We will stage our file once again.

$ git add Load.txt

We can use the git reset command to unstage the Load.txt file, as shown below.

$ git reset Load.txt

Let’s check our index.

$ git status

git status after git reset

And there you have it. Our file is no longer staged for commit.

In conclusion, Git allows us to unstage a file we have staged for committing. We can use either the git reset command or the git restore command with the --staged flag.

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 Add