How to rebase after squashing commits in the original branch?

Deadly 提交于 2019-11-29 12:03:30

问题


I have

A--B--C master
       \
        D branch0

Then I squash B and C into B'. How do I rebase branch0 such that it looks like this:

A--B' master
    \
     D branch0

回答1:


Use the --onto argument to git rebase, which changes the baseline that git replays work on.

git checkout branch0

At this state you should still see C in your git history.

git rebase --onto B' C

This translates to: Take all commits in my current branch since commit C (In your case that's just D) and play back on top of B'

You can also use the optional <branch> argument to do both the checkout and rebase at once:

git rebase --onto B' C branch0



回答2:


One quick way I can think of is,

git checkout branch0

Note down the sha of the D commit by git log

git checkout master

Now rename the branch0 to branch1

git branch -m branch0 branch1

A--B' master \ D branch1

Now delete branch1

git branch -D branch1

Create a branch0 again as follows. git checkout -b branch0

A--B' - master&branch0 After the above command master and branch0 are same. Only change we need to make in branch0 is get the commit D.

git cherry-pick D

Now branch0 looks as follows

A--B' master \ D branch0



来源:https://stackoverflow.com/questions/35901915/how-to-rebase-after-squashing-commits-in-the-original-branch

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