1

My coworker got himself into some trouble yesterday. He moved master about 100 commits behind origin/master (I'm not sure how he did that, probably with this), then he committed. The result was that master now was at his new commit and the previous commit was 100 commits behind origin/master. It looked like this:

master * 
       |
       |  * origin/master 
       |  *
        ... ~one hundred commits below
       |  *
       \  *
         \
          *

I didn't feel confident in how to fix this so the way we did it is by throwing away the repo and cloning it again. What's the right way to fix this mistake?

His intent was to create a new branch named hotfix 100 commits before origin/master and commit these changes to it. I don't think he really wanted to move master at all.


I'll take a stab at fixing this:

git checkout master
git branch hotfix
#still on master
git reset --hard HEAD~1
git branch -f master <commit of origin/master>
Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

2 Answers2

1

I'd just create a branch called hotfix and reset master to origin/master:

$ git checkout master
$ git branch hotfix
$ git reset --hard origin/master
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • That `reset` is intimidating. I'll have to research how it works in that case. Just for my info, would my attempt also work? – Daniel Kaplan Mar 05 '15 at 21:42
  • @DanielKaplan: I think so. You're doing a `reset` also, though. – mipadi Mar 05 '15 at 21:48
  • Yeah that's fair, I am much more comfortable with a reset to HEAD~1 (which I do frequently) than a reset to a remote branch (which I've never done) – Daniel Kaplan Mar 05 '15 at 22:02
0

Disclaimer: I'm not a git expert. This is the best I could come up with.

First, create a new branch with existing fixes:

git checkout -b oops <commit-id>

Then, switch back to master and remove the selected commit with:

git checkout master
git rebase --onto <commit-id>^ <commit-id>

Example: https://gist.github.com/cmattoon/13aa5c8d711a7ffc7899

Curtis Mattoon
  • 4,642
  • 2
  • 27
  • 34