Anthony Accioly
Anthony Accioly

Reputation: 22481

Rebase head of master on top of another branch (both already pushed)

I know that there are a lot of similar questions in stackoverflow, but before asking this I did some research and couldn't find a direct answer my question.

My situation is the following: I've a private repo, and am working with another developer. So we are two developers working with a private repository, which means that push -f is not the end of the world. This repository will soon go public and, at this point we rater fix mistakes and do a little rework to change history (so that, once the repository goes public, people can work with a solid base of working commits).

Yesterday I've pushed a commit into master that, for some reason, only works in my machine and in our dev server, but not in my colleague machine nor the production server.

So we forked a branch (album) from a previous commit. My colleague commited several times on this branch. He them pushed the branch to the remote repository (we needed to do this in order to publish his working version to the production server) and we ended up with something like this:

master: (previous commits) - C1 - C2 (my "troubled" commit)
                              \
album:                        C3 - C4 - C5 - C6  

Under normal circumstances I would checkout master (in which my troubled commit C2 is the head) and merge album into it.

But this would leave C2 on the history, plus, since we can't figure out for the life of ours, why my commit broke the environment we feel like it is better to "reaply" the commit the other way around.

What am I thinking of doing:

Desired result:

master: (previous commits) - C1 - C3 - C4 - C5 - C6 - C2' 

Where C2' should be fixed and working in all machines.

So, am I thinking straight? If not, how would the git gurus out there deal with this?

Thank you kindly.

Upvotes: 1

Views: 208

Answers (3)

joshtkling
joshtkling

Reputation: 3538

If I'm understanding correctly, I believe what you want here is:

  • git checkout master
  • git reset --hard <sha1 of C1>

At this point C2 is momentarily orphaned and won't be reachable via your branch names. Now you can merge your branch album in as you would normally.

Now, to fix the problem of the orphaned C2 commit, you'll want:

  • git checkout master
  • git cherry-pick <sha1 of C2>

This will leave your master branch looking like:

master: (previous commits) - C1 - C3 - C4 - C5 - C6 - C2

Upvotes: 1

jthill
jthill

Reputation: 60575

That'll do it, I'd get that result this way:

$ git checkout album
$ git cherry-pick master
$ git checkout -B master

and the ref cleanup's identical.

Upvotes: 2

Zhube
Zhube

Reputation: 504

You can rebase on top of master, yes. That should work fine for where the branch is. Personally, I would clone the repo in a new directory, do a dry run of the rebase, and confirm your results. If all is as desired, repeat the steps in your normal working directory.

Cheers

Upvotes: 0

Related Questions