Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update an outdated branch against master

Tags:

git

github

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
like image 207
flossfan Avatar asked Oct 02 '22 23:10

flossfan


2 Answers

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.

like image 81
Magnus Bäck Avatar answered Oct 13 '22 10:10

Magnus Bäck


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)

like image 36
Stijn Haezebrouck Avatar answered Oct 13 '22 11:10

Stijn Haezebrouck