How can you undo the last git add?
GitGit Problem Overview
Is it possible to unstage the last staged (not committed) change in git? Suppose there were a lot of files in the current branch, some staged, some not. At some point, some foolish programmer accidentally executed:
git add -- .
...instead of:
git checkout -- .
Can this programmer now unstage his last changes with some magical git command? Or should he have committed before experimenting in the first place?
Git Solutions
Solution 1 - Git
You can use git reset
. This will 'unstage' all the files you've added after your last commit.
If you want to unstage only some files, use git reset -- <file 1> <file 2> <file n>
.
Also it's possible to unstage some of the changes in files by using git reset -p
.
Solution 2 - Git
You cannot undo the latest git add, but you can undo all add
s since the last commit. git reset
without a commit argument resets the index (unstages staged changes):
git reset
Solution 3 - Git
So the real answer to
> Can this programmer now unstage his last changes with some magical git command?
is actually: No, you cannot unstage just the last git add
.
That is if we interpret the question as in the following situation:
Initial file:
void foo() {
}
main() {
foo();
}
First change followed by git add
:
void foo(int bar) {
print("$bar");
}
main() {
foo(1337);
}
Second change followed by git add
:
void foo(int bar, String baz) {
print("$bar $baz");
}
main() {
foo(1337, "h4x0r");
}
In this case, git reset -p
will not help, since its smallest granularity is lines. git
doesn't know that about the intermediate state of:
void foo(int bar) {
print("$bar");
}
main() {
foo(1337);
}
any more.
Solution 4 - Git
You could use git reset
(see docs)
Solution 5 - Git
At date git prompts:
use "git rm --cached <file>..." to unstage
if files were not in the repo. It unstages the files keeping them there.use "git reset HEAD <file>..." to unstage
if the files were in the repo, and you are adding them as modified. It keeps the files as they are, and unstages them.
At my knowledge you cannot undo the git add --
but you can unstage a list of files as mentioned above.
Solution 6 - Git
-
Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy:
git reset head <file>
-
Reset the file to the last state from HEAD, undoing changes and removing them from the index:
git reset HEAD <file> git checkout <file> # If you have a `<branch>` named like `<file>`, use: git checkout -- <file>
This is needed since
git reset --hard HEAD
won't work with single files. -
Remove
<file>
from index and versioning, keeping the un-versioned file with changes in working copy:git rm --cached <file>
-
Remove
<file>
from working copy and versioning completely:git rm <file>
Solution 7 - Git
If you want to remove all added files from git for commit
git reset
If you want to remove an individual file
git rm <file>
Solution 8 - Git
You can use
git reset
to undo the recently added local files
git reset file_name
to undo the changes for a specific file
Solution 9 - Git
Depending on size and scale of the difficultly, you could create a scratch (temporary) branch and commit the current work there.
Then switch to and checkout your original branch, and pick the appropriate files from the scratch commit.
At least you would have a permanent record of the current and previous states to work from (until you delete that scratch branch).