1

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

Below is the scenario: enter image description here

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: enter image description here

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?

Community
  • 1
  • 1

2 Answers2

3

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).

Useless
  • 64,155
  • 6
  • 88
  • 132
1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
BugHunter
  • 119
  • 6
  • Thanks for your answer. Yes I was referring to that part of the book. The book also mentioned that two commits that have the sam eauthor, date, does the the author mean C4` and C6? –  Sep 25 '19 at 13:36
  • 1
    I think they mean C4 and C4'. You will have them both in your logs, because you have downloaded both histories done by that person - merged one and rebased one. By the way, I would suggest learning Git from other source. I feel pretty confident when using Git, but I am struggling to follow with this book. Check out Atlassian tutorials for example: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase – BugHunter Sep 25 '19 at 13:52
  • could you also have a look at my new question that has relationship to this question https://stackoverflow.com/questions/58100856/git-pull-create-a-merge-commit-after-rebasing –  Sep 25 '19 at 14:38
  • Sure, added my answer. – BugHunter Sep 25 '19 at 15:21