1

All the related questions ask about moving commits from the main branch to another branch.

My problem is somehow opposite:

M(n)-M(n+1)-M(n+2)-M(n+3)
\
B(1)-B(2)-B(3)-B(4)-B(5)

Consider that M stands for a commit on master and B for a commit on a secondary branch. The contents of the parantheses are indexes.

I want to merge the branches and move B(5) on top of the main branch, so right after M(n+3), disregarding any change made from M(n) to M(n+3).

I'm avoiding to use git rebase, as the structure of my repo is the same as that of a shared repo, and I don't know what will happen if I would rewrite the history with git rebase in this context.

bsky
  • 19,326
  • 49
  • 155
  • 270
  • I'm not sure how you actually _do_ this with commands, but it sounds like what you want is to _relabel_ the branches, so that B is now master and M is discarded, and I know that that's _possible_. – zwol Mar 04 '14 at 15:43

3 Answers3

0

Try this sequence of git commands:

 git checkout M
 git reset --hard HEAD~3
 git merge B
tijs
  • 566
  • 4
  • 12
0

If your commits are pushed to a remote repository, you can't just move things around. If the commits on branch M are only local then you can rollback to M(n) and merge branch B to it which will get you what you want.

However, if the commits have been pushed you should avoid this because you have rewritten history and will have to do a git push -f. If anyone else has pulled the changes then it will cause a lot of problems. In this case you will need to revert the changes from M(n+1) to M(n+3).

Using this answer: https://stackoverflow.com/a/1470452/498699

git revert --no-commit M(n+3)
git revert --no-commit M(n+2)
git revert --no-commit M(n+1)
git commit

Now you can merge branch B and have the changes applied correctly and anyone else that has pulled the changes will not have any difficulties pulling your changes.

Community
  • 1
  • 1
Schleis
  • 41,516
  • 7
  • 68
  • 87
0

If you just want to have B(5) (and none of B(1) to B(4)) right after M(n+3) then you can just run

git checkout master
git cherry-pick B(5)

If you want all of B(1) to B(5) after M(n+3) but do not want to change the history of B, no worry, just create a new C branch.

git checkout -b C_branch B_branch
git rebase --onto master `git merge-base C_branch master` C_branch

That will give

M(n)-M(n+1)-M(n+2)-M(n+3)
\                        \
B(1)-B(2)-B(3)-B(4)-B(5)  C(1)-C(2)-C(3)-C(4)-C(5)
hlovdal
  • 26,565
  • 10
  • 94
  • 165