0

I set up a GitHub project (origin).

I then cloned this repo into a bunch of different machines (ex. my local computer, a production environment, a staging environment, ect).

I make changes on my local machine, and push to origin.

Eventually, I merge my development branch into origin/master.

I then want to pull these changes from origin/master into my production environment, so;

root@production$ git --version
git version 1.7.9.5
root@production$ git remote
origin
root@production$ git branch -r
  origin/HEAD -> origin/master
  origin/master
  ...

from here, I've tried everything;

git fetch
git checkout master
----------------------------------
git checkout origin/master
----------------------------------
git checkout -b origin/master HEAD

But none of them worked (ie, the changes were never reflected). It wasn't until I did

git pull origin master

that it finally worked. But I don't understand why I can't just checkout the master? Everything I'm reading on SO says to use checkout (git checkout remote branch) - I don't get it :S

Community
  • 1
  • 1
Joney
  • 309
  • 1
  • 2
  • 8

1 Answers1

1

git fetch fetches objects and updates your remote origin/master; it doesn’t change any local branches. To merge origin/master back into master after a fetch, you would use merge:

git merge origin/master

git pull is just a git fetch plus a git merge. You usually won’t even have to specify the branch and remote names; if you’ve set the upstream for the branch to origin, you can just run:

git pull

and it will assume you mean to fetch the upstream of the current branch and merge it into the current branch.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thanks for your response. I think I get that - but why can't I checkout origin/master? Perhaps doing a `pull` is the right approach, but now I'm just curious why I can't checkout? – Joney Mar 31 '14 at 01:43
  • @Joney: You should be able to check out `origin/master`, but you’ll be in the “detached HEAD” state, and changes you make won’t automatically be reflected on `master`. (You’re not on `master` at that point.) – Ry- Mar 31 '14 at 02:01
  • I have seen the detached HEAD message before. Whats the correct git command to use to do this? – Joney Mar 31 '14 at 05:01
  • @Joney: To go into the detached HEAD state? That’s it. `checkout` on something that’s a pointer to a commit, but not a local branch. (e.g. a remote branch or a commit hash.) – Ry- Mar 31 '14 at 14:35