1

What's the difference between git checkout <remote>/<branch> vs git checkout <branch>? When do you need to use git checkout <remote>/<branch>?

Boon
  • 40,656
  • 60
  • 209
  • 315
  • 1
    Related to / possible duplicate of http://stackoverflow.com/questions/25670173/why-does-git-tell-me-not-currently-on-any-branch-after-i-run-git-checkout-ori – jub0bs Dec 19 '14 at 18:51
  • `git checkout /` is almost never what you want to do – Andrew C Dec 19 '14 at 20:15

2 Answers2

3

The key thing to understand is that remote branches are normal branches with the name <remote>/<branch>. They are just references (labels) pointing at a commit. The main difference is you can't commit to them.

Why would you check out a remote branch? To inspect the state of the project upstream, maybe to try building it. Otherwise, you wouldn't.

There are uses for referencing a remote branch...

  • git diff remote/branch to look at the differences between your branch and the remote.
  • git log remote/branch..HEAD to look at changes with the remote.
  • git branch -f branch remote/branch to throw out all your local changes.
Schwern
  • 153,029
  • 25
  • 195
  • 336
2

git checkout <remote>/<branch> will check out the commit that git rev-parse refs/remotes/<remote>/<branch> resolves to, and leave you in "detached HEAD" state.

git checkout <branch> will check out the local branch given, or, depending on your configuration, might automatically create a local branch named <branch> that is set up to track <remote>/<branch> for you, and then check out that local branch. It will not leave you in a "detached HEAD" state, but on a local branch (unless it fails for some reason, like you have it configured not to create branches automatically, and the named branch doesn't exist).

chepner
  • 497,756
  • 71
  • 530
  • 681
twalberg
  • 59,951
  • 11
  • 89
  • 84