简体   繁体   中英

How can I rebase after a merge?

I'm reading the Pro Git book and get stuck on rebasing.

Below is the scenario: 在此处输入图像描述

so two branches have already merged. Then the book says after we make a merge, we can still change mind to rebase C4 to C5 as: 在此处输入图像描述

But I tried to replicate the rebasing and found out that after a merge is made, I cannot rebase anymore, if I try:

git checkout branch_on_c4

git rebase branch_on_c5

I got no new commit of C4' , and the message is:

First, rewinding head to replay your work on top of it

Applying: add

Using index info to reconstruct a base tree

Falling back to patching base and 3-way merge

No changes -- Patch already applied**

If I didn't merge two branches first, I have no problem with rebasing C4 on C5. But once I have done the merge, I cannot rebase anymore, so how does the author or what command does the author use to do the rebasing after merging?

tl;dr

How can I rebase after a merge?

You don't need to, and that isn't what the book said. Just throw the merge away if you didn't like it, and rebase instead .


Then the book says after we make a merge, we can still change mind to rebase

No, it doesn't.

It says

... if instead of doing a merge when [ this happens ] we run git rebase teamone/master ...

That is, instead of merging, not after .

So, you'd either have to run fetch and identify the potential problem in advance, or run a normal merge pull , decide it's a mess and throw it away to start again with:

git reset --hard HEAD@{1}

(I tend to avoid the HEAD~1 syntax for merges, because I don't want to have to think about which parent is which - the reflog syntax for "previous value of HEAD" is simpler IMO).

I assume that you are referring to Fig. 45 and Fig. 46 here: Git Branching Rebasing

Next, the person who pushed the merged work decides to go back and rebase their work instead; they do a git push --force to overwrite the history on the server. You then fetch from that server, bringing down the new commits.

That person most likely did something to undo the merge, for example:

git reset --hard HEAD~1

Then it was possible to execute rebase and push --force .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM