80

Let's say I have a private topic branch called develop with 2 commits ahead of master.

What does git pull origin master do?

Pull everything from the remote master in the local develop and merge it? Pull everything in the local master branch and merge it?

And is there a way to update master from develop without git checkout master first?

mrj
  • 1,571
  • 1
  • 10
  • 7
Bite code
  • 578,959
  • 113
  • 301
  • 329

2 Answers2

110

git pull origin master pulls the master branch from the remote called origin into your current branch. It only affects your current branch, not your local master branch.

It'll give you history looking something like this:

- x - x - x - x (develop)
   \         /
    x - x - x (origin/master)

Your local master branch is irrelevant in this. git pull is essentially a combination of git fetch and git merge; it fetches the remote branch then merges it into your current branch. It's a merge like any other; it doesn't do anything magical.

If you want to update your local master branch, you have no choice but to check it out. It's impossible to merge into a branch that's not checked out, because Git needs a work tree in order to perform the merge. (In particular, it's absolutely necessary in order to report merge conflicts and allow you to resolve them.)

If you happen to know that pulling into master would be a fast-forward (i.e. you have no commits in your local master branch that aren't in origin's master) you can work around, as described in this answer.

Community
  • 1
  • 1
mrj
  • 1,571
  • 1
  • 10
  • 7
  • 2
    You can push into a local not-checked-out branch if it is fast-forwardable. But that's quite a hack ;) – knittl Jan 05 '12 at 18:11
  • @knittl: True! Pushing is elegant in that it checks to see if it's fast-forwardable for you. I like the `update-ref` solution because you can leave the right trace in the reflogs, and I'm obsessive like that. – mrj Jan 05 '12 at 18:19
  • @e-satis: Encouraging new users to vote on questions is great... but worth answering and worth an upvote aren't necessarily the same thing. (Though your question is both.) – Cascabel Jan 05 '12 at 18:26
  • @jefromi: yeah, unfortunate shortcut. – Bite code Jan 05 '12 at 23:20
  • *It's impossible to merge into a branch that's not checked out* - after all this time, finally it's become clear to me... – Theta Jan 14 '22 at 11:42
  • I've been doing `git fetch origin master:master` from a feature branch, doesnt that update my local master branch without it being checked out? – hogan Sep 05 '22 at 12:28
11

Once you commit you changes into your branch by using

git add -A
git commit -m <message>

You can then do:

git pull origin master

into your branch and that will keep your commits on top of the master pull. Your branch will now be even with master + your commits on top. So, you can now do:

git push

and git will push your changes, together with the master commits into you branch. You can easily then merge that into master on Github.

Vlad
  • 161
  • 1
  • 6