Chris Maes
Chris Maes

Reputation: 37832

git revert commit, back to staged and unstaged

I regularly use

git add -p

to select which parts I want to commit. Once I selected what I wanted; I proceed to using

git commit -a

and finalize my commit. Now I realize that I should NOT have used the -a option, because now I have undone the work done with git add -p (that is, splitting my changes in multiple commits).

The only way I know to revert this, and make it back the way I wanted is:

git reset HEAD~
git add -p
git commit

Is there any way that I don't have to do git add -p again; performing the exact same operations to split my changes into multiple commits? So I want to undo the git commit -a and go back to the staged and unstaged files just before (that is the way git status was just after using git add -p).

Upvotes: 0

Views: 470

Answers (1)

Hexana
Hexana

Reputation: 1135

To recover files or code that was lost due to a mistaken commit, you can look at the dangling blobs. To do this, run:

$ git fsck --unreachable

This gives you a list of the dangling blobs ... for example:

unreachable blob 070204aa62dc0ef612f922a02d06d3....
unreachable blob 821c4ca73cb5c99f7fa5f23358e3a9....
unreachable blob b91c74fc1b5c0f8eb8ccfd1a8024c6....

You then check out one of those blobs to check for your missing code:

git show 070204aa62dc0ef612f922a02d06d3....

This lets you view the content in the dangling blob, but does not give you the name of the file. You can then write that content to a file by using the first 5 characters of the sha1

git show d06d3 > myfile

This puts 'myfile' in the directory you are currently in. Myfile at this point is a .txt which you can then copy and paste/convert to the correct MIME type.

Hope this helps.

Upvotes: 1

Related Questions