Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update topic branch with upstream changes on master?

Tags:

git

git-rebase

I start some work on a topic branch

    •-•-• < topic
   /
•-•       < master

I push the topic branch

$ git push origin topic

Someone else pushes changes to master

    •-•-• < origin/topic
   /
•-•-•—•   < origin/master

How do I update my local master and rebase my topic?

History should look like this

        •-•-• < topic
       /
•-•-•—•       < master

What I am trying

; update master
$ git checkout master
$ git fetch origin
$ git merge --ff-only origin/master

; rebase topic
$ git checkout topic
$ git rebase master

The problem

All of my commits on topic are seen as uncommitted. So when I try git push origin topic, I get

 ! [rejected]        topic -> topic (non-fast-forward)
error: failed to push some refs to '/path/to/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The solution?

Please note, I do not want to merge my topic branch with master. I'd simply like to get my local repo updated without having to merge branches unnecessarily.

like image 617
Mulan Avatar asked Dec 17 '12 17:12

Mulan


2 Answers

; update master
git checkout master
git pull --rebase origin master  

; rebase topic
git rebase master topic

; push topic (force)
git push -f origin topic
like image 113
the.malkolm Avatar answered Oct 03 '22 00:10

the.malkolm


Let me rework your example for clarity.

    C-D-E < topic, origin/topic
   /
A-B < master, origin/master

Then someone does work.

    C-D-E < topic, origin/topic
   /
A-B-F-G < origin/master
  ^
master

You fetched F & G from origin, and then rebased topic onto master. So now your repository looks like this.

    C-D-E < origin/topic
   /
A-B-F-G < master, origin/master
       \
        C'-D'-E' < topic

And this is the problem. origin/topic at E can not be fast-forwarded to topic at E'. Rebase is really only meant for commits that have not been pushed to origin. Since you pushed C, D, and E to origin/topic already, you would have to rewrite history on the remote repository. Hence the error. So you really have three options:

  1. Stop pushing a topical branch. If it's only you who is working on topic, there's no need to push it. Just keep rebasing topic on top of master, and when done, fast-forward merge master to topic & push master. Delete local topic branch. Voila!

  2. Merge topic & master. If you need to collaborate on a topical branch, you should probably suck it up and merge.

  3. Force the remote rebase:

    git push origin topic -f

    This will force origin/topic to E'. Except by re-writing history in the remote repository, you'll have fire and brimstone, human sacrifice, dogs and cats living together, mass hysteria... and your fellow developers not liking you very much. Not recommended at all.

like image 35
Mike Monkiewicz Avatar answered Oct 02 '22 22:10

Mike Monkiewicz