Home > other >  git merge into the middle of the main commits where feature branch started
git merge into the middle of the main commits where feature branch started

Time:08-20

I couldn't find any similar questions for this precise requirement. Assume I have a feature and a main branch. We have some new commits in the main branch after creating the feature branch. What I want to do is merge into the main at the precise commit in which feature branch was created, as shown in the image below. In other words, feature commits come before main commits.

enter image description here

CodePudding user response:

If you mean to merge the feature branch and apply the changes of the second to last revision of main in a single shot (the way the chart is displaying as the desired result), it will require some hacking

git checkout main~2
git merge --no--ff featureBranch
git cherry-pick main~
git reset --soft @~ # set HEAD pointer in merge revision we just created
# changes from main~1 are in index
git commit --amend -m "merge and changes from main~1 in a single revision"
git cherry-pick main
# if you like the result
git branch -f main
git checkout main

And that's it.

CodePudding user response:

git rebase feature main
git replace --graft $(git rev-list feature..main|tail -1) \
        $(git merge-base feature main@{1} feature
git filter-branch

The rebase gets the content you want, the replace sets up the weird ancestry, the filter-branch bakes it in.

  •  Tags:  
  • git
  • Related