flossfan
flossfan

Reputation: 10854

Update an outdated branch against master

I am working on a new feature in a new branch, and meanwhile the master has changed.

I would like to update my branch to reflect the changes to master, with the following constraints:

  1. I've already pushed the feature branch, so I don't want to use rebase in case that causes problems for anyone else who may be looking at it.
  2. I don't want to add any of my feature changes into master yet, only to my local branch.

There are lots of answers suggesting that in this situation one should use rebase, but I'm nervous about doing this in case it causes problems for anyone who's already pulled the branch.

Can I just do this?

git checkout mybranch
git merge origin/master
git push origin mybranch

Upvotes: 6

Views: 1400

Answers (3)

Stijn Haezebrouck
Stijn Haezebrouck

Reputation: 397

Your reasoning about the danger of rebase is correct: if someone pulled your branch, he might indeed get into problems, as a rebase rewrites some of the git history. Merge is always safe.

Your sequence is totally correct, and very typical when working with feature branches. Before you ever merge a feature branch into the master, you should merge the master in your feature branch every now and then, so that it does not starts deviating too much from the master. A large deviation could result in many merge conficts when finally merging into the master branch (when the feature is ready). By regularly merging the master branch into your feature branch, you resolve the (possible) conflicts much earlier, the conflicts are smaller and easier to understand.

In your process, line (2) is where you must resolve the conflicts (if any)

You were stating that you want your feature changes only local. But as you pushed/pushes your feature branch, the feature changes are also on the remote server (on a remote branch, not in the master)

Upvotes: 2

Robert
Robert

Reputation: 36773

Yes, you can. Moreover, that is the way to update your branch and track that event in the history (because it makes a merge commit). And, as you say, rebase could cause problems if you have already pushed your branch (anyone won't be able to do a normal push, they will need to force push, even you)

Upvotes: 0

Magnus Bäck
Magnus Bäck

Reputation: 11571

If you've pushed your branch elsewhere you shouldn't rebase it, so a merge is appropriate. Your command sequence at the end is fine.

Upvotes: 3

Related Questions