How to Undo git add
Git does not automatically include changes in a commit: they have to be explicitly added to the next commit, with the git add
command.
But sometimes you might change your mind and decide that a certain file should not be part of the next commit. In this short article, we'll answer the question of how to undo adding a change to the staging area, after having used git add
before.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Using git restore to Undo git add
Luckily, there's a simple way of undoing a git add
: you can simply use the git restore --staged
command on the affected file:
$ git restore --staged index.html
This will remove the file from Git's staging area, making sure it is NOT part of the next commit.
If, at the same time, you also want to discard any local changes in this file, you can simply omit the --staged
flag:
$ git restore index.html
This will undo any modifications in this file since you last committed it. Please be careful with this command: undoing uncommitted local changes cannot be undone!
Tip
Undoing "git add" in Tower
In case you are using the Tower Git client, undoing a "git add" is as simple as unchecking the "Status" checkbox for the affected file:
Using git reset to Undo git add
The restore
command is a quite recent addition to Git (in version 2.23). If you're using an older Git version, you have to resort to the git reset
command to achieve the same results:
$ git reset index.html
Just like git restore
, this will remove the file from Git's staging area and thereby NOT include it in the next commit. The local changes themselves are not affected by this command.
Learn More
- Check out the chapter Working on Your Project in our free online book
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.