Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover lost commits after git submodule update and checkout?

Tags:

git

This is a variant on similar questions, where commits end up in a headless branch. I am adding this question to cover this particular case. In my case, the issue is the following:

  • I did a git submodule update, which left the repository in a headless state (and I forgot to checkout again to master)
  • I committed code, several commits in fact
  • As I was about to push to github, I realized I was in a headless state, and switched to the master branch (the github app makes it soooo easy)

The result: my commits were now in git limbo.

like image 938
charles Avatar asked Sep 27 '13 15:09

charles


People also ask

How do I restore a dropped commit?

The process for recovering a deleted commit is quite simple. All that is necessary is to use `git reflog` to find the SHA value of the commit that you want to go to, and then execute the `git reset --hard <sha value>` command.

Where is git submodule commit stored?

It is stored in Git's object database directly. The tree object for the directory where the submodule lives will have an entry for the submodule's commit (this is the so-called "gitlink").

Does git submodule update pull?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


1 Answers

Based on answers here and there, I found my way out using git reflog:

> git reflog
6b0da0d HEAD@{0}: rebase finished: returning to refs/heads/master
6b0da0d HEAD@{1}: pull --rebase --progress --prune --recurse-submodules=on-demand origin: check
d55ecfb HEAD@{2}: checkout: moving from fed7916169d740644dbbd9ea48e2d2cd510ce32d to master
fed7916 HEAD@{3}: commit: more secret stuff.
818bf20 HEAD@{4}: commit: incredible stuff I am doing, hopefully won't end up in limbo.
...etc...

The commit fed7916 is the one I want to merge to master. For this, I simply typed:

> git merge fed7916

The merge went without problem (it should since it was anyway branched from where master was at) and all my commits were now reachable again, and ready to be pushed to github.

like image 132
charles Avatar answered Oct 21 '22 20:10

charles