Luck is with you, provided you have not disabled git's "reflogs": commit B
is still there. (Even if you have removed the airbags and seatbelts it's probably still there anyway. :-) Reflog entries preserve commits for at least 30 days by default.)
If you know the SHA-1 ID of commit B
:
$ git reset --hard <id of B>
will get it back.
If you don't know the ID of B
, you must find it, or direct git to find it, but fortunately that's easy1 since they are in the reflogs:
$ git reflog # shows the reflog for HEAD
$ git reflog master # shows the reflog for branch master
You can then copy-and-paste the ID, or use the HEAD@{1}
style syntax that you'll see in the reflog output:
bf8be34 master@{0}: reset: moving to bf8be34bb970c875585952efc5c09898f98e1d10
d106870 master@{1}: commit: ...
Note that --hard
will, as always, wreck any uncommitted work you have in the work-tree.
1Well, "easy", if scanning through a bunch of SHA-1s and one-line commit messages to find the right one is "easy". In an active repository, finding a buried reflog entry is tricky. If you do a lot of rebasing and amending, the same commit message gets repeated a lot. Fortunately in this case it's going to be ...@{1}
.