0

I have the following git history (from oldest A to newest D commits):

mybranch:
commit A
commit B
...
merge main into mybranch
commit C
commit D
...

I'd like to modify it to:

mybranch:
merge main into mybranch
commit A
commit B
...
commit C
commit D
...

How can I do this? I thought to use git rebase -i but that does not seem to allow reordering commits while preserving the merge commit.

phd
  • 82,685
  • 13
  • 120
  • 165
yasgur99
  • 756
  • 2
  • 11
  • 32
  • You can merge `main` again into your branch. It works and it should produce the outcome that you desire. – axiac Aug 09 '23 at 16:30
  • You could rebase, removing the merge, and then merge again? – evolutionxbox Aug 09 '23 at 16:31
  • @axiac if I merge again, I think the merge commit will be most recent in history. I'd like it to be moved further back. – yasgur99 Aug 09 '23 at 16:33
  • The question is misleading. I though that the most recent commits are on top. – axiac Aug 09 '23 at 16:34
  • Further back? The question makes it seem like you want it to be the most recent commit. You can pause the rebase, merge, and then continue? – evolutionxbox Aug 09 '23 at 16:34
  • https://stackoverflow.com/a/4784082/7976758 `git rebase -i --rebase-merges` – phd Aug 09 '23 at 16:35
  • I updated to clarify the history order. Sorry about that. – yasgur99 Aug 09 '23 at 16:35
  • Your branch is a working branch. You should squash it into a single commit before making a PR (or before merging it back into `main`). The commits on your branch, their order or when you have merged `main` in it should not matter. – axiac Aug 09 '23 at 16:36
  • @phd the git rebase documentation says -i and rebase-merges are not compatible together – yasgur99 Aug 09 '23 at 16:36
  • @axiac I cannot squash my branch based on rules of our CI strategy. – yasgur99 Aug 09 '23 at 16:36
  • Re-read https://git-scm.com/docs/git-rebase#_rebasing_merges – phd Aug 09 '23 at 16:41
  • You can squash (it's a local operation) but you probably cannot push with `--force`. If this happens, you won't be able to push after you move the merge commit either. If you did not already push then you can do whatever you want on your local repo. – axiac Aug 09 '23 at 16:41

0 Answers0