11

I have (commits were created in alphabetical order)

origin/topic  A-B-C-D-W-X

topic         A-B-C-D-Y-Z

Is there a way i can get my topic branch to look like

A-B-C-D-W-X-Y-Z

without introducing a merge commit?


Ordinarily I'd just do

$ git checkout topic
$ git fetch origin
$ git merge --ff-only origin/topic

But since Y and Z were committed after W and X, a fast forward will not work.

I suspect some clever rebasing or cherry picking might work but I can't wrap my head around it :{

Mulan
  • 129,518
  • 31
  • 228
  • 259

3 Answers3

13

You can try with:

git checkout topic
git pull --rebase origin topic

Check the fetch man page notes about the rebase option.

I would recommend using something like git-smart, which will do a fast forward (which is faster) when possible, and if not, then a rebase. It will also stash local changes if you have any and give you a summary of the remote changes that were pulled.

mamapitufo
  • 4,680
  • 2
  • 25
  • 19
  • This is simply incredible... now to spend time actually understanding how it's working... – Mulan Nov 26 '13 at 17:02
  • 1
    @naomik remember, you can think of Git as just a tree of commits with post-it notes (representing references and branches) attached to them. You're simply copying/moving commits around and re-applying the post-it notes when you rebase. –  Nov 27 '13 at 04:15
7

As an alternative to mamapitufo's answer, you can also do the following, which will give you the exact same result:

git fetch origin
git rebase origin/topic topic

The last topic parameter is optional if you already have it checked out.

Community
  • 1
  • 1
3

If you already have topic checked out and origin/topic fetched which is tracking origin/topic (sounds like you do), you could just:

git rebase

I gave mamapitufo an up vote.

user3021843
  • 384
  • 3
  • 4