How to undo "git add"

Key takeaways:

  • The staging area (or index) is where changes are prepared before committing, separating modified files from the repository.

  • The reason for the “undo” is that we may accidentally stage files that are not yet ready for commit.

  • Unstaging methods:

    • git reset <filename>: Unstage a file

    • git reset: Unstage all files

    • git restore --staged <filename>: Unstage a specific file while keeping changes

    • git restore --staged .: Unstage all files

When editing code in Git, we sometimes commit changes that we shouldn’t. The great thing is that Git allows us to fix this effortlessly. In this Answer, we’ll discuss different commands that can be applied to the unstaged files present in our git repository, as well as explain what happens to your changes when you use these commands.

Understanding the staging area

Before diving into how to undo git add, it’s essential to understand the staging area (also known as the index) in Git:

  • Working directory: This is where we’ll make changes to files.

  • Staging area: This is where we prepare changes to be committed. When we run git add, we move changes from the working directory to the staging area.

// to stage a file
git add filename
// to stage all changed files
git add .
  • Repository: This is where your committed changes are stored. After you run git commit, the changes in the staging area are saved to the repository.

How to undo git add

Sometimes, we may move changes to the staging area unintentionally and will need to undo this operation. For this, we can use the following two methods:

Both of these methods unstage the files from the staging area.

Method 1: Using the git reset command

  • To remove a single file from the staging area:
git reset filename 
  • To remove all files from the staging area:
git reset

When calling reset, the changes made to the files before executing git add will be available.

Method 2: git restore --staged

  • To reset a single file from the staging area:
git restore --staged filename

The following command is used to unstage all changes in the staging area while keeping the modifications intact in your working directory:

git restore --staged 

Hands-on practice

Hands-on practice is essential for truly understanding and mastering new concepts. Just as Donald Knuth suggested:

“The only way to learn about new things is to do them; the way to learn usability is to make something usable.”

Let’s run the following commands in the terminal given below to first add files to the git local repository and then reset them from the staging area:

  • We will first create three files with some text, such as README.md, first.html, and last.html.

  • To add files for Git to track, use the git add filename command.

  • Next, run the git reset command to remove a file from git add.

cd git
git init
echo "# My README.md file" > README.md
echo "# My second file" > first.html
echo "# My third file" > last.html
git add .
git ls-files
git reset filename
git ls-files
Commands for updating the commit message

Let’s execute the above commands one by one and see how to undo git add:

Terminal 1
Terminal
Loading...

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is revert in Git?

The git revert command is also used to undo changes made to a repository. However, it does not remove these changes from Git history. It inverts the changes made after the specified commit. Then, it creates a new commit with the resulting changes.


How do we run git clean?

The git clean command is used to remove untracked files from your working directory.


How can we stage files again after unstaging?

We can stage files again after unstaging using the git add filename or git add . command.


How do we check which files are staged?

We use the git status command to check for the staged files.


How can we learn more about Git commands?

We can use the git help command to learn more about Git commands. Also, check out our detailed Answer on some important git commands.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved