Git rebase, skip merge-commits

和自甴很熟 提交于 2019-12-03 11:33:20

问题


Starting with

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

I would like to end up with

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

So far the best I have is

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 even though this is a dubious workflow.


回答1:


Simple case

If the state of your repo is

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

and you want to arrive at

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

you should use git cherry-pick, not git rebase -i (no need to juggle with interactive rebase, here):

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

General case

Correct me if I'm wrong, but I understand what you mean by general case as

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

In the following, I'm assuming that is indeed what you mean.

As you've rightfully noted in your comment, the approach outlined above doesn't scale very gracefully as the number of commits to manually cherry-pick increases:

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

However, you can get git rev-list to automatically generate the 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 --reverse flag, so that commits get cherry-picked in the desired order.

You can pass the output of that 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 would yield

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


来源:https://stackoverflow.com/questions/25886147/git-rebase-skip-merge-commits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!