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
There are two options that will really do your job easy.
git reset HEAD <file>?
git rm --cached <file>?
Let’s remember first what
git add <file> it actually does:
- If it
<file>was not previously tracked,
git addadds it to the cache , with its current content.
<file>it’s already tracked, the
git addcurrent 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.
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