How to Undo 'Git Add': Reverting Staged Files Before Commit : A Step-by-Step Guide

In Git, the git add command stages changes for a commit, but if you mistakenly add files or reconsider their inclusion, Git offers a straightforward method to unstage these files.

Undoing git add for All Files

If you want to unstage all files you've added, you can use one of the following commands:

  • Using a basic git reset without any arguments:
git reset
  • Specifying HEAD with the reset, which refers to the current commit:
git reset HEAD
  • Using git reset with no arguments, which implicitly targets HEAD:
git reset 

Any of these commands will unstage all the files you've added, but won't alter the actual changes made to the files.

Undoing git add for a Specific File

If you only want to unstage a specific file, you can do so by including the file's name in the reset command:

  • Basic reset with the filename:
git reset <filename>
  • Using HEAD with the filename:
git reset HEAD <filename>
  • Using @ (a shortcut for HEAD since Git v1.8.5) with the filename:
git reset @ <filename>

Replace <filename> with the actual name of the file you want to unstage.

Understanding the Impact

Using the git reset command in this way only affects the staging area and doesn't change the contents of your files. It's a safe way to undo the staging before you commit, allowing you to review and reselect the files for your commit.

Note on HEAD

In Git, HEAD is a reference to the last commit of the current branch. When you use git reset, you’re essentially telling Git to make the staging area look like the last commit (HEAD), without altering the working directory.

Subscribe to GIT.WTF!?!

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe