Reputation: 681
I have a feature branch off develop. I started using the practice of rebasing my feature branch with develop instead of merging as the benefits of a linear commit history was appropriate for my project which had a few upstream and downstream repositories.
I'm in the situation where someone merged develop into the feature branch and did some more commits then pushed to the remote. I would like to be able to get the point where I remove the merge commit and cherry pick the subsequent commits on the branch. This is with the knowledge that other people working on the branch will have to basically delete their local ones and pull down the revised one. I work in a small team and this is manageable.
Is the best method to do this an interactive rebase selecting all the commit bar the merge commit?
So git rebase -i commit-sha-before-merge
I know this will most likely result in a broken build as the commits that occurred after the merge relied on the code in the merge. I will resolve this with finally rebasing my feature branch with develop.
Upvotes: 12
Views: 4962
Reputation:
As torek points out in the comments, there is more than one way to do this in Git. Say, for example, that you have a commit graph like this:
develop *-------*
\ \
feature *---*---*---*---*
X M^ M Y^ Y
X is the first commit on your feature branch, M is the merge commit with develop, and Y is the last commit on the feature branch.
This relies on the fact that rebasing a branch B onto another branch A is equivalent to merging branch A into B. In this case, we'll use two rebases, one to rebase feature branch commits before the merge commit, and the other to rebase the commits after the merge:
# Create a temporary branch at the commit right before the merge commit M
git checkout -b temp M^
# Rebase onto the develop branch
git rebase develop
# Now rebase the remaining commits after M onto the temp branch
git rebase --onto temp M feature
This will produce the following commit graph
X M^ Y^ Y
*---*---*---*---*---*
^ ^ ^
develop temp feature
So now you can delete the temp branch with git branch --delete temp
.
Here is how you can achieve the same result using cherry-picks, like what torek suggested:
# Temporarily hard reset feature to the develop branch
git checkout feature
git reset --hard develop
# Cherry-pick all commits between X to M^.
# The start of the cherry-pick range is exclusive, i.e. it doesn't include
# the starting point X^.
git cherry-pick X^..M^
# Cherry-pick all commits between M to Y
git cherry-pick M..Y
Upvotes: 14