TieDad
TieDad

Reputation: 9929

How to rebase at both local and remote repositories?

This is a follow up question of how to handle this git rebase issue?.

I'm developing a new feature, so I create a new branch, like this:

A--B--C--D              master branch
         \
          \-1--2--3     my feature branch

But given people is keeping adding code to master branch, I want to always to get latest code of master branch. Note, my feature is in a stand along directory, so there will be no conflict when rebase or merge to master. What I want is:

A--B--C--D--E--F
               \
                \-1--2--3--4

I get answer from the last question as linked about using git rebase origin/master.

But trouble comes again when I try to push my feature branch to remote:

$ git push ssh HEAD:ntfdev
To [email protected]:yyyyy/yyyy.git
! [rejected]        HEAD -> ntfdev (non-fast-forward)
error: failed to push some refs to '[email protected]:yyyy/yyyy.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.

I think the failure is because my rebase the feature branch in local but remote feature branch is not rebased, so they are not consistent.

How to rebase at both local and remote?

Upvotes: 0

Views: 286

Answers (1)

amavroudakis
amavroudakis

Reputation: 2362

You should not need to rebase the remote branch at the same time. Your only worry should be to keep up to date your local feature branch.

You should do that like this if you like rebasing:

git checkout master
git pull --rebase #refresh master using rebase
git checkout featureBranch
git rebase master #replay all changes on the feature branch on top of the master branch

If you do the above at regular intervals you will have your feature branch as a continuation of master only local.

Now at the point you want your changes to be pushed to server on the master branch, you should do the above and only after that do:

git checkout master
git merge featureBranch #because you have rebased, this will only fast forward the feature branch pointer to where the master branch pointer points to
git push

That sequence will maintain a linear history if this is something you value.

If your feature branch is pushed to the server and more than one developers are working on it, then there are two things you need to be doing: 1) Make sure you get the latest version of the remote feature branch locally at regular basis and for sure before you push any changes to the remote feature branch, 2) Make sure the remote feature branch stays up to date with master

for the first you need to only do:

git checkout featureBranch
git pull --rebase

That should happen every time you want the latest code of the remote branch and definitely before a push to the remote feature branch happens. We first pull to bring the local feature branch up to date with any changes in the remote feature branch (fixing any conflicts on the way) and the push to the server our change (now that are all nicely merged)

for the second you need to what I describe above.

All developers working on the feature should do the first often. I suggest assign the responsibility to one developer on doing the second at least once a day. After this developer does the update from master he/she notify the rest of the team to do refresh their local feature branches from the remote feature branch. They

I hope that helps.

Upvotes: 1

Related Questions