5

This is my situation:

  • I have started a new branch (let's call it foo) and did some work on that,

  • I have pushed foo to remote

  • something needed to be done on master, so I switched to master, did the fixes and pushed,

Now: how do I update foo with master? Because I need to work on foo, but I want it to be aligned with the changes in master. I understand I am not supposed to rebase a branch that has been published...

Thanks!

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • 1
    Why not simply use `merge`? – Maroun Apr 24 '15 at 11:55
  • Merge from master to branch, test, correct, commit, then merge from branch to master. – cup Apr 24 '15 at 11:58
  • 1
    possible duplicate of [Get changes from master into branch in git](http://stackoverflow.com/questions/5340724/get-changes-from-master-into-branch-in-git) – Joe Apr 24 '15 at 12:05

2 Answers2

16

Good that you know you should not rebase and force-push a published branch.

You need the changes of master into foo? The most natural thing to do here is to do just that. Merge the changes of master into foo.

Your situation:

o---o---o - master
     \-o---o---o - foo

then:

$ git checkout foo
$ git merge master

leads to a new merge commit m in foo:

o---o---o---------- - master
     \             \
      \-o---o---o---m - foo

It is clear from the history what you did, and probably why. You can even mention what you needed from master in the commit message of m.

Gauthier
  • 40,309
  • 11
  • 63
  • 97
0

Either merge master from foo

git checkout foo
git merge master

Or, if foo is not used by other developers and you have authorization to force a push on the remote, then you can rebase it and force-push it :

git checkout foo
git rebase master
git push -f remote

There is no danger in rebasing and force-pushing already published commits as long as you know that nobody except you has planned to work on or from those commits until they fetch your rebased-then-published commits. Next time your colleagues fetch from the remote, they will see that foo was updated by force, just that.

larsks
  • 277,717
  • 41
  • 399
  • 399
hdl
  • 1,028
  • 1
  • 9
  • 23