22

Can I recover the following commit after a hard reset?

Steps:

1) $ (master) // ....made a bunch of changes to files thinking I was working on a branch
2) $ git checkout -b 001-branch  // copy changes to a branch to work with
3) $ (001-branch) // make some more changes to files
4) $ (001-branch) git commit -a -m 'added and changed stuff'
// at this point I was just going to pull force master to latest then rebase my 001-branch off of original master (not the stuff I had modified)
5) $ (001-branch) git checkout master
6) $ (master) git reset --hard HEAD
7) $ (master) git pull
8) $ (master) git checkout 001-branch // go back to my branch and rebase my changes
9) $ (001-branch) // oops...my changes were all kiboshed and I don't see the commit I did per git lg

Any way out of this mess to recover my changes?

genxgeek
  • 13,109
  • 38
  • 135
  • 217
  • 2
    A few questions: (1) Was the commit in step 4 actually successful? (2) Did you actually do a rebase at step 8 or not yet? (3) What does "all kiboshed" mean? Do you see the commit made in step 4 at all, is it mangled in some way, or is it just missing? (4) Do you see your commit in `git reflog` on 001-branch? The reflog is the usual way to dig yourself out of this sort of thing. – Greg Hewgill Nov 11 '13 at 04:01
  • Does this answer your question? [Recover from git reset --hard?](https://stackoverflow.com/questions/5788037/recover-from-git-reset-hard) – Liam Jan 30 '20 at 08:53

4 Answers4

26

To see all the changes made to your 001-branch, you can do git reflog 001-branch, however, chances are what you think you did in 001-branch maybe you did another branch, so you probably need to look into all the changes with git reflog.

FelipeC
  • 9,123
  • 4
  • 44
  • 38
19

Does git reflog show print the hash of the missing commit? If yes, then git checkout -b recovery-branch commitId will create a new branch pointing to the missing commit. You can then merge as needed.

chwarr
  • 6,777
  • 1
  • 30
  • 57
7

Every time git does something drastic like changing or rewinding branches, it records that in reflog. In other words, carefully inspect output of git reflog, it will tell you all transitions you branch ever had. You can then use git show commit_id to inspect and git checkout commit_id to get back.

mvp
  • 111,019
  • 13
  • 122
  • 148
4

Honestly I don't get what happened.

git reflog prints what commits a branch was pointing at. You can use it to find the SHA1 sum of recent local commits.

Jokester
  • 5,501
  • 3
  • 31
  • 39