Bill Door
Bill Door

Reputation: 18926

How to keep up to date with a parent branch of a remote branch

I would like to keep a topic branch up to date with it's parent branch. This works wonderfully with a local topic branch using rebase to the parent branch:

git checkout topic
git rebase master

However, if topic is remote branch then it seems that rebasing to master is in conflict with checking into the remote branch.

What is the best way to keep up with changes to the master in a remote branch.

Here is what I've been doing. I always seem to end up with multiple conflicts and multiple "branches" when rebasing remote branches to master.

I use the standard construct when working with remote branches:

git checkout master
git checkout -b topic
git push origin topic
git branch --set-upstream topic origin/topic

I continue to work on topic, make commits, and push to the origin.

# edit some files
git commit -a
git push

At some point master has been updated and I need to incorporate those changes into my topic branch.

git checkout master
git pull
git checkout topic
git rebase master

Great. Now push that update.

$ git push
To [email protected]:duane/branchtest.git
 ! [rejected]        topic -> topic (non-fast-forward)
error: failed to push some refs to '[email protected]:duane/branchtest.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

Curiously, the log looks something like this:

$ git log --graph --format=oneline
* 31f4b133a4a9983528ba309e6c161e09e8061f84 Change 1 for topic 2.
* 2dc45b91856748a9c6adaf3579a7b8b2cf90a6ae Change 3 for file1.
* db97959ec75267bf94d97af6e8b2a8a762c61b25 Add topic2.txt.
* 08e6cc4b14a23dad86c2d283ee4acc1cebc775ef Change 2 for file1.
* e707b9571b2aa4d01073f156abb407fec15a195a Change 1 for file1.
* 2002670efda4d8c83150a4cc1c25fac62e37814d Add file1.txt.
* d3a6eae43e4824f455872622491c2ca861f96d4f Begin the repo.
$ git pull
Merge made by recursive.

And afterwards looks like

$ git log --graph --format=oneline
*   18ba5265e2836e1a7884341dbedb8b2cc8ab9727 Merge branch 'topic' of github.com:duane/branchtest into topic
|\  
| * 0e5f3022c7f087ed4dd70d11a0cba250300be05e Change 1 for topic 2.
* | 31f4b133a4a9983528ba309e6c161e09e8061f84 Change 1 for topic 2.
* | 2dc45b91856748a9c6adaf3579a7b8b2cf90a6ae Change 3 for file1.
|/  
* db97959ec75267bf94d97af6e8b2a8a762c61b25 Add topic2.txt.
* 08e6cc4b14a23dad86c2d283ee4acc1cebc775ef Change 2 for file1.
* e707b9571b2aa4d01073f156abb407fec15a195a Change 1 for file1.
* 2002670efda4d8c83150a4cc1c25fac62e37814d Add file1.txt.
* d3a6eae43e4824f455872622491c2ca861f96d4f Begin the repo.

Change 1 for topic 2 was the change that I made. It is now listed twice.

A push of course ends up with this mess on my remote topic branch.

An even larger problem is that this extra merge point is repeated every time a rebase to master occurs; even with no changes on master!

Where did I go wrong? How can I keep up to date with the master branch while maintaining a remote branch?

Upvotes: 4

Views: 5236

Answers (1)

Karl Bielefeldt
Karl Bielefeldt

Reputation: 49028

If I understand correctly, you're wanting the remote topic branch to exactly mirror your local branch, but changes only being done on your local computer?

In that case you should git push --force whenever you make a local change to your topic branch. However, this shouldn't be done if you're sharing your topic branch with anyone else. In that case, you should skip the rebase and just do a merge from master to keep up to date with it. It's not as "pretty," but it's a lot more reliable.

Upvotes: 2

Related Questions