Git rebase, skip merge commits

Beginning with

hack---F1----M1----F2 (feature) / / C1-----C2----C3 (master) 

I would like to finish with

  hack---F1----M1----F2 (feature) / / C1-----C2----C3---F1'---F2' (master) 

So far the best I have

 git checkout feature git checkout -b temp git rebase -i --onto master hack temp * Big drawback: manually remove the merged-in C2 and C3 from list of commits * git checkout master git merge temp git branch -d temp 

I hope someone can answer, although this is not an easy workflow.

+13
git rebase
source share
1 answer

Simple case

If the state of your repo

  hack---F1----M1----F2 [feature] / / C1-----C2----C3 [master] 

and you want to come to

  hack---F1----M1----F2 [feature] / / C1-----C2----C3----F1'----F2' [HEAD=master] 

you should use git cherry-pick , not git rebase -i (you don't need to manipulate interactive rebase here):

 git checkout master git cherry-pick <commit-ID-of-F1> <commit-ID-of-F2> 

General case

Correct me if I am wrong, but I understand what you mean by the general case as

cherry-pick, on top of master , all commits without merging between hack (exclusive) and the tip of feature (inclusive).

Further, I assume that this is really what you mean.

As you rightly noted in your comment, the approach described above does not scale very well, as the number of commits per sample manually increases:

  hack---F1---F2--- .... --- F68--M1---F67---...---F99 [feature] / / C1-------------C2---------------C3 [master] 

However, you can get git rev-list to automatically generate a list of revisions of interest using

 git rev-list --reverse --no-merges --first-parent <commit-ID-of-hack>..feature 

Edit: you also need the --first-parent flag to avoid collecting commits such as C1 and C2 , and the --reverse flag --reverse that commits are selected in the selected order.

You can pass the output of this command to git cherry-pick :

 git checkout master git cherry-pick 'git rev-list --reverse --no-merges --first-parent <commit-ID-of-hack>..feature' 

which will give

  hack---F1---F2--- .... --- F68--M1---F67---...---F99 [feature] /                / C1-------------C2---------------C3---F1'---F2'---...---F99' [HEAD=master] 
+13
source share

All Articles