Let’s assume by mistakenly you added file to git by using the command: git add, and you want to undo “git add before git commit?” Even this is the usual problem that you face in our day-to-day work as a Developer or DevOps or SysOps.
So what’s the real pullback from git add
?
There are two options that will really do your job easy.
git reset HEAD <file>
?
or
git rm --cached <file>
?
Let’s remember first what git add <file>
it actually does:
- If it
<file>
was not previously tracked,git add
adds it to the cache , with its current content. - If
<file>
it’s already tracked, thegit add
current content (snapshot, version) is stored in the cache. - In Git, this action is still called add, (not just update it), since two different versions (snapshots) of a file are treated as two different items: hence, we are indeed adding a new item to the cache so that it will eventually be committed later.
Considering this, the question is :
I added files by mistake using the command, git add , how I do undo before git commit ?
It ‘undo’ to remove the file (not just the current content) from the tracked items. If so, everything is fine git rm --cached <file>
.
And we could run as well git reset HEAD <file>
. I prefer this because it works in both scenarios: it also undoes when we mistakenly added a version of an already tracked item.
Example:
1- File1.txt created and by mistake index to git using git add command.

2- Undo the git add using git reset file1.txt and track file again using git status
