2

Let say I have following git structure

1--2--3--11--12 (master-branch)
       \
        4--5--6--7--8--9--10--13--14 (custom-branch)

How can I get into the following git structure?

1--2--3--4--11--12 (master-branch)
          \
           5--6--7--8--9--10--13--14 (custom-branch)
Maroun
  • 94,125
  • 30
  • 188
  • 241
xcode
  • 1,637
  • 3
  • 16
  • 25
  • Probably something along the lines of rebasing master onto `4` – Mad Physicist Feb 01 '18 at 07:12
  • @Maroun. Only if you didn't read the question. – Mad Physicist Feb 01 '18 at 07:13
  • @MadPhysicist True, didn't pay enough attention. – Maroun Feb 01 '18 at 07:13
  • @MadPhysicist rebasing only work on same branch right? – xcode Feb 01 '18 at 07:14
  • @Maroun doesn't work like that – xcode Feb 01 '18 at 07:14
  • 1
    @xcode Not at all. See [here](https://git-scm.com/book/en/v2/Git-Branching-Rebasing). – Maroun Feb 01 '18 at 07:14
  • It would be easier to visualize if you put all the custom commits on one line and elevated 11,12 above that, instead of the other way around. – Mad Physicist Feb 01 '18 at 07:15
  • @xcode. I hope you don't mind, but I edited your diagrams to make the solution obvious without altering the meaning in any way. I'll revert immediately if you object. – Mad Physicist Feb 01 '18 at 07:21
  • @MadPhysicist This edit is too much to be done instead of OP, please let them decide if changing the visualisation. – Maroun Feb 01 '18 at 07:22
  • @Maroun. You didn't let OP decide if they wanted to revert. I did not alter the meaning of OP's post in any way with my edit. What is your actual objection? – Mad Physicist Feb 01 '18 at 07:25
  • @MadPhysicist Maybe OP prefer the current visualisation, your suggestion as a comment is perfectly fine, but please let OP decide whether to include it or not. Such edits are probably rejected if they were reviewed due to "*clearly conflicts with author's intent*". – Maroun Feb 01 '18 at 07:28
  • 2
    @MadPhysicist I think my visualization is better since it emphasis on how commit `4` changed, from only present on `custom-branch` to also available on `master-branch`. – xcode Feb 01 '18 at 07:31
  • 1
    @Maroun. Looks like you were right. I had no intention of starting a revision war either and I'm sorry I wasted your time. – Mad Physicist Feb 01 '18 at 07:36
  • @MadPhysicist no waste at all! Your attempt to help is highly appreciated, please keep trying were you feel it helps. We do learn from each other's mistakes. – Maroun Feb 01 '18 at 07:37

2 Answers2

4

Just rebase master on the custom-branch up to the 4 commit:

git checkout master
git rebase custom-branch~8

By inspection, all you want to do here is to make 1--2--3--4 the new common base of both branches.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • According to OP's visualisation, you'll also need to drop the commit from `custom-branch`, no? – Maroun Feb 01 '18 at 07:25
  • 1
    @Maroun Look closely; the custom branch doesn't change here. It's only written differently. – Tim Biegeleisen Feb 01 '18 at 07:26
  • @MadPhysicist It's only my opinion, feel free to revert it back, I won't start a revision war (or take it to meta, and let the judge decide ;)) – Maroun Feb 01 '18 at 07:29
  • @TimBiegeleisen Thank You, but what if the amount of commits on `custom-branch` are in the hundreds or even thousands, it's not possible to count it backward, any other method instead of using `~8`? – xcode Feb 01 '18 at 08:04
  • @xcode I think you can also rebase to a specific commit. Try `git rebase `, and replace `` with the hash of the `4` commit. Note that even in this case there is no avoiding knowing what that commit is. If not, then you have some detective work to do. – Tim Biegeleisen Feb 01 '18 at 08:12
1

If these commits have been pushed to remote already, I would just rather merge commit 4 to master.

git checkout master
git merge <4-commit-id-here>

But that is not what you asked for, so what you could do is to cherry-pick the commit you want on master:

git checkout master
git cherry-pick <4-commit-id-here>

Then if these are only local branches or you are perfectly sure you want to be rewriting history also on remote, you can reorder the commits with git rebase interactive:

git rebase -i HEAD~4

Rebase interactive shows you a list of commits which you can reorder.

After reordering the master you have new commit ID for the commit 4. So you need to rebase custom-branch on top of that:

git checkout custom-branch
git rebase <new-commit4-id-here>

Rebase should by default ignore the old commit 4 from custom branch as it does not introduce any new changes on top of new commit 4. So that is it.

If your changes were already on the remote, you need to do force push (assuming here you have tracking for both branches in place).

First push the custom-branch: git push -f

And then push the master branch:

git checkout master
git push -f

But generally rewriting history on remote should be avoided. More on this e.g. in here: How do I properly force a Git push?

user232548
  • 435
  • 4
  • 11