0

I have squashed 7 commits (using git rebase -i HEAD~7) into a single commit and pushed the changes to bitbucket server. Now, I realized I need to push the 7 commits in original state.

I tried git reflog and git reset --hard HEAD@{7} But can only see one commit.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Saurabh
  • 975
  • 2
  • 12
  • 27

2 Answers2

3

In git reflog, there should be a line like,

8847515 HEAD@{7}: rebase -i (start): checkout HEAD~7

Now it may have become HEAD@{8} or HEAD@{9} in your repository. Find the stash entry right below this line, which is the original HEAD before the interactive rebase starts. It might be like 0874c8a HEAD@{10}: commit: foo bar. Reset to this commit instead,

git reset 0874c8a --hard
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
2

The easiest way undo rebase:

git reset --hard ORIG_HEAD
# or
git checkout -b recoveredBranch ORIG_HEAD

This works only for last rebase.

Now HEAD@{7} will point to 7 states back of the head. It is hard to predict where it will point to. It depends what have you been doing. Every reset/checkout means that you have to increase this number.

As a last resort find last commit before rebase in a reflog and use its hash:


git checkout -b recoveredBranch a0a0ab0a
Marek R
  • 32,568
  • 6
  • 55
  • 140