bny
bny

Reputation: 285

Recover from git rebase gone wrong

Here is what happend : I was working on the master branch of my repo when I noticed at push time that my 3 commits where rejected because "Bob" pushed directly to master.

In order to solve this, I created a branch "secondary" by doing

git checkout -b secondary
git push -u origin secondary

At this point I made a mistake, I forgot to git pull origin master (bob patches) into secondary. I did a git rebase where I deleted the 9 commits done by "bob" then did a git push -f. The result : All bob commits are gone from both branches Forever!

Fortunately right before that failure, I had a second machine where it was way behind on master and I had 2 commits to push after git pulling. pull failed because of merge conflicts and I did a reset and a git fetch and left it there thinking I would do it after fixing bob issue. That machine current status is :

On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 22 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

I didnt run any commands since. these 22 commits contain "Bob" commits as well. Is there any way I can get that old "fetched" data into master/secondary again ?

Any help appreciated

Upvotes: 3

Views: 2664

Answers (2)

Philip Oakley
Philip Oakley

Reputation: 14101

That old fetch data will still be there in the refs/remotes branches (which are held locally!).

Do a git branch -a to see all those remotes and their branches on the machines, then simply git checkout -b <newbranchname> <remote>/<branch> and you will be good to go.

Now is the time to review / refresh all that 'didn't quite sink in' stuff about DAGs, refs, rtb's, etc. along with the DWIMing - see git help revisions and git help -g.

--

rtb's = remote tracking branch - a local branch which tracks (has a copy of) the branch on the remote, usually kept 'read only'.

DWIM - Do what I mean. So / will get the 'ref/remotes/remote/branch' for you instead of 'refs/heads/branch' etc.

Upvotes: 3

MOS
MOS

Reputation: 137

If neither one of you still have the commits locally and they are not on the server I suggest using reflog (https://git-scm.com/docs/git-reflog) to try to find and restore them, if in luck they haven't been garbage collected yet.

Upvotes: 2

Related Questions