git rebase basics

Viewed 71173

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

3 Answers

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

Related