Git

How to Undo Git Add Command

While using Git, it is common for programmers to create and add new project files to their repositories, either remote or local, during the project development. Users associate files with commits; they often add files or folders from the working directory to the staging area immediately before committing them to the repository. But it is required to undo the adding operation due to wrong tracking or missing data.

This guide will explain how to undo the Git add command.

How to Undo Git Add Command?

To undo the Git add command, firstly, we will move to the repository and display the existing content of it. Then, create and add a file to the staging area. Next, commit all made changes to the repository and check the status. Lastly, undo the add command by executing the “$ git restore –staged <file-name>” command.

Let’s move ahead to perform the above-provided operation!

Step 1: Navigate to Git Local Directory

Move to the desired local directory by utilizing the provided command:

$ cd "C:\Users\nazma\Git\Demo3"

Step 2: List Content

Next, run the “ls” command to list the content of the current repository:

$ ls

Step 3: Create File

Create a new file in the repository using the below-given command:

$ touch file6.txt

Step 4: Track File

Now, add the created file to the staging area:

$ git add file6.txt

Step 5: Check Status

Check the repository status by executing the “git status” command:

$ git status .

Step 6: Undo Git Add

Now, run the “git restore” command with the “–staged” option to undo the add file operation:

$ git restore --staged file6.txt

Step 7: Check Status

Again, check the repository’s current status through provided command:

$ git status .

According to the below output, the “file6.txt” tracked file is reverted into the working directory from the staging area:

We have offered the procedure to undo the Git add command.

Conclusion

To undo Git add command, firstly, navigate to the repository and list the existing content. Next, create a file and add it to the staging area using the “$ git add <file-name>” command. Then, commit all added changes to the repository and check the status. After that, undo the add command operation by utilizing the “$ git restore –staged <file-name>” command. This guide illustrated the method to undo the Git add command.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.