When you deliberately making a commit, it is almost inevitable that you’re like “Oops, I need to cancel this git add”.
This article shows some ways of undoing “git add” you’ve run by mistake.
Cancelling “git add”
Here’s some command examples. I’ll add a little follow-up later.
// 1) Reset the index entirely
git reset HEAD
// 2) Reset one or some files
git reset file.txt
git reset HEAD file.txt
// 3) Reset all of the files.
git rest .
git reset HEAD .
“git reset” without specifying a branch name means the same as “git reset HEAD”, which is nothing more than how “git reset” is meant to work.
And (3) does almost the same as (1), but if you know “git reset” well enough, it means somewhat different thing in terms of what git is doing. (Let’s talk about this another time)
What exactly does “git reset” do?
I thinks this helps you understand what’s going on when you run add or reset.
Basically, “git add” is duplicating some/all your changes you’ve made on your work tree, to index. The index is also referred as “stage”, and that is going to be the commit when you run “git commit”.
Now, “git reset” unstages any changes you have added onto the index, which means it’ll bring back the index to a clean slate.
Some of you might notice that reseting the index is not exactly cancelling a git add execution. Yes, you’re right, if you have done “git add” multiple times before “git reset”, it’ll erase any of your changes added to index.
So, the bottom line is, “git reset cancels all your changes added to index”.
OK, This is it. I bet you can enjoy git add and reset a little more now. Take care.