git rebase basics
Asked Answered
P

3

92

I have started using git rebase recently and am not 100% certain I'm doing it right. For the sake of the question, there are two branches in origin, master and next, which was branched from master.

Since last sync between the two, master had 2 commits and next 6:

$ git log --oneline origin/next..origin/master
59b5552 master commit #2
485a811 master commit #1

$ git log --oneline origin/master..origin/next
4ebf401 next commit #6
e9b6586 next commit #5
197ada0 next commit #4
4a2c3c6 next commit #3
040a055 next commit #2
84537bf next commit #1

When I checkout next and execute git rebase -i origin/master, I get the following:

$ git status
# On branch next
# Your branch and 'origin/next' have diverged,
# and have 8 and 6 different commits each, respectively.

And finally after doing git pull --rebase, the two commits from master are in next:

$ git log --oneline origin/next..next 
8741d09 master commit #2
485a811 master commit #1

Questions:

  1. Is this correct approach?
  2. Why are there 8 and 6 different commits until pull --rebase is run?
  3. Is it possible to simplify the flow?

Much obliged :)

Pga answered 19/7, 2012 at 14:44 Comment(0)
T
102

Let's start from the beginning. Here's a diagram of your original state:

A-B-C  (master, origin/master)
 \
  D-E-F-G-H-I  (next, origin/next)

When you checked out next and rebased next onto origin/master, it created 6 new commits after the two that are already on origin/master. These new commits have "master commit #2" (C in my diagram) as their ancestor, not their original ancestor where origin/master and origin/next diverged (A in my diagram), so their hashes will be different. I believe this is why you'll see that next has 8 different commits from origin/next: the 2 from origin/master and the 6 "rehashed" commits that were on origin/next.

After git checkout next ; git rebase -i origin/master, you should have this:

A-B-C  (master, origin/master)
 \   \
  \   D'-E'-F'-G'-H'-I' (next)
   \
    D-E-F-G-H-I  (origin/next)

You can see that next does have 8 commits that aren't on origin/next, and origin/next does have 6 commits that aren't on next. Granted this is just according to the SHA-1 hashes of the commits. The actual content should match very closely if you git diff origin/next next -- the diff should just show the changes from B and C (as labeled in the diagram).

When you do git pull --rebase while still on next, it fetches changes from the source (the remote origin/next) and rebases the current branch (next) onto that remote. This causes the changes that were in the next but not in origin/next to appear after origin/next on the new next branch. It should look like this:

A-B-C  (master, origin/master)
 \
  D-E-F-G-H-I  (origin/next)
             \
              B'-C' (next)

If this is what you wanted the history graph to look like, then you've succeeded.

However, I suspect you really wanted things to look like the middle diagram, especially if next is a feature branch where you're working on the next piece of the project and master is for stable code and small bug fixes. If so, then you should have done git push instead of git pull --rebase to make the remote reflect your version of history instead of the other way around.

Trilingual answered 19/7, 2012 at 17:48 Comment(7)
I assume git rebase --pull is much like git pull --rebase. It does a fetch and then a git rebase @{u} Well, that's a lie, but it is an easy way to think about it. But the point is that your local branch is reset to @{u} and then all local commits on your old branch before the reset are replayed on top of what upstream has. This allows a trivial fastforward push upstream.Tuition
Assuming git rebase --pull was a typo and should have been git pull --rebase, then I think that would reorder next into D'-E'-F'-G'-H'-I'-B'-C'. Does that sound right? If so, I'll edit my answer accordingly.Trilingual
After git pull --rebase you will get A-D-E-F-G-H-I-B'-C'. The g-p-r will always force your local branch (next) to contain all of the commits on @{u} (origin/next) and then anything which is unique to next will be replayed (probably cherry-picked) on top. Congratulate git for being smart enough to not attempt to create D"-E"-F"-G"-H"-I"Tuition
Really sorry for the typo, it should be git pull --rebase. Thank you for the explanation, I now understand what happens. Is the flow I'm doing correct? git rebase and then git pull --rebase or is there some other way?Aureaaureate
@DavidKuridža: There is no particular reason why you need to do either command. If next was at ADEFGHI and your goal is ADEFGHIB'C' then the only command you need to run is git checkout next; git cherry-pick ...masterTuition
David, I think rebase followed by pull --rebase is self-defeating, since it changes the history in one direction and then goes back the other way. The real question to answer before you run any commands (other than git fetch to update origin/*) is: What do I want the history to look like when I'm done? Once you know that, then the actions you take should lead towards that goal.Trilingual
It turned out my understanding of rebase was wrong, thanks to all these comments I believe it should be fine now. I am really grateful to all of you!Aureaaureate
C
4

Start with the very simple steps for rebasing your branch with the master; Name;

git-rebase

Synopsis;

git rebase [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
        [<upstream>] [<branch>]
git rebase [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
        --root [<branch>]
git rebase --continue | --skip | --abort | --edit-todo

Description; Assume the following history exists and the current branch is "sample":

 A---B---C sample
         /
    D---E---F---G master

From this point, the result of either of the following commands:

git rebase master
git rebase master sample

would be:

A'--B'--C' sample
                 /
    D---E---F---G master

NOTE: The latter form is just a short-hand of git checkout sample followed by git rebase master. When rebase exits sample will remain the checked-out branch.

If the upstream branch already contains a change you have made (e.g., because you mailed a patch which was applied upstream), then that commit will be skipped. For example, running ‘git rebase master` on the following history (in which A’ and A introduce the same set of changes, but have different committer information):

A---B---C sample
         /
    D---E---A'---F master

will result in:

 B'---C' sample
              /
D---E---A'---F master

All these were the diagramatic understanding of the rebase process. Once you resolve the conflicts being found after typing git rebase master resolve the conflicts and type git add -u to add the changed codes to the repository. after that perform the command git rebase --continue and continue resolving the conflicts and and repeating the command ;

git add -u 

and

git rebase --continue 

until no conflicts being found. At last the final command will be ,

git push --force origin sample(your branch name)
Caterwaul answered 26/12, 2013 at 10:37 Comment(3)
for external refernce follow kernel.org/pub/software/scm/git/docs/git-rebase.htmlCaterwaul
Please realign your diagrams to make it clearBaresark
@Sam, in your answer, there are git commands i.e., git rebase [-i | --interactive] [options] under Synopsis section. Can you tell what is the rule to understand those parameters i.e., what does | mean? What does [...] mean ? Where are the rules mentioned in the online doc you mentioned in the comment ?Selfpropulsion
C
1

I don't always do git rebase, but when I do, I use this "hammer":

o---o---o---o---o  A
     \
      o---o---o---o---o  B
                       \
                        o---o---o  C

git rebase -r B C --onto A    # C should be branch, not hash

o---o---o---o---o  A
    |            \
    |             o'--o'--o'  C
     \
      o---o---o---o---o  B

No more stressing about what is the best way to do git rebase for each case ("Should I use rebase -i? Should I use cherry-pick?") and spending time looking them up.

Of course this is not for everyone, like for example, in a case where we want to split a commit, some people don't like "doing a bunch of funky work on a new branch". But some people, like me, "have a phobia of interactive approaches".

Fear not the man who does 1000 ways of rebase once, but fear the man who does one way of rebase 1000 times. ~Not Bruce Lee

Cohlette answered 10/5, 2022 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.