Undo “git add” – How to Remove Added Files in Git

How to Remove Added Files in Git

The git add command allows you to stage changes before committing them to the repository. However, sometimes you may accidentally stage a file or decide that you no longer want specific files included in the next commit. This is the case where you need to remove added files in Git.

In this article, we’ll walk through the steps to unstaged files added using git add and explore different methods to revert this action.

There’s plenty to explore, so let’s dive in!

Understanding git add

Before diving into how to undo git add, let’s briefly discuss what the command does. The git add command stages files, preparing them to be included in the next commit. When you run git add filename , Git takes a snapshot of the file and moves it from your working directory to the staging area. This staging area is like a holding space for changes that will be committed together.

Here’s a typical scenario:

 # git add example.txt && git status

Output.

On branch main
Changes to be committed:
  (use "git restore --staged filename..." to unstage)
	new file:   example.txt

The file example.txt is now staged and ready to be committed. But what if you added it by mistake or changed your mind? That’s where undoing git add comes in.

Undoing git add for a Single File

To unstage a single file that you’ve added, you can use the git reset command with the HEAD reference. This will remove the file from the staging area but keep the changes in your working directory.

1. Unstage a Single File:

 # git reset HEAD example.txt

Output.

Unstaged changes after reset:
M	example.txt

2. Check the Status:

 # git status

Output.

On branch main
Changes not staged for commit:
  (use "git add filename..." to update what will be committed)
  (use "git restore filename..." to discard changes in working directory)
	modified:   example.txt

The file example.txt has been removed from the staging area, but the changes remain in the working directory. This is useful if you added the file by mistake or need to make further modifications before committing.

Undoing git add for Multiple Files

If you’ve added multiple files and want to unstage all of them, you can use git reset without specifying a file. This will unstage all files that were added.

1. Unstage All Files:

 # git reset HEAD

Output.

Unstaged changes after reset:
M	example.txt
M	another-file.txt

2. Check the Status:

 # git status

Output.

On branch main
Changes not staged for commit:
  (use "git add filename..." to update what will be committed)
  (use "git restore filename..." to discard changes in working directory)
	modified:   example.txt
	modified:   another-file.txt

All files that were previously staged have now been unstaged. This command is helpful when you’ve added multiple files but want to reconsider or modify them before committing.

Undoing git add with git restore

Another method to undo git add is to use the git restore command, which is specifically designed for restoring working tree files.

1. Unstage a file Using git restore:

 # git restore --staged example.txt

2. Check the Status:

 # git status

Output.

On branch main
Changes not staged for commit:
  (use "git add filename..." to update what will be committed)
  (use "git restore filename..." to discard changes in working directory)
	modified:   example.txt

The git restore –staged command removes the file from the staging area, similar to git reset HEAD filename . The key difference is that git restore is a more modern command introduced to simplify restoring files in both the working directory and the staging area.

Recovering from Mistakes

Sometimes, you might accidentally unstage a file or remove it from the index and later realize it was a mistake. You can recover these files using git stash or git checkout.

1. Stash Changes:

 # git stash

Output.

Saved working directory and index state WIP on main: 1234567 Example commit message

This command saves your changes temporarily and cleans your working directory. You can later apply the stashed changes using git stash apply.

2. Recover with git checkout:

 # git checkout HEAD example.txt

3. Check the Status:

 # git status

Output.

On branch main
nothing to commit, working tree clean

This command will restore the file from the last commit, discarding any changes made since.

Conclusion

Understanding how to undo git add is valuable for anyone using Git. Whether you need to unstage a single file or multiple files, commands like git reset and git restore provide powerful ways to manage your staging area.

FAQs

1. How do I recover files after accidentally unstaging them?

You can recover files using git stash to temporarily save the changes or git checkout HEAD file.txt to restore the file from the last commit.

2. Can I unstage all files at once?

Yes, using git reset HEAD without specifying a file will unstage all files that were previously added with git add.

3. What happens if I unstage a file and then commit?

If you unstage a file using git reset or git restore, that file will not be included in the commit. Only the files still staged will be committed.

4. Does git reset remove the changes from my working directory?

No, git reset HEAD file.txt only removes the file from the staging area. Your changes remain in the working directory.

5. Can I undo git add after committing?

No, but you can use git reset --soft HEAD^ to undo the commit and unstage the files.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva