What's the difference between git checkout <remote>/<branch> vs git checkout <branch>? When do you need to use git checkout <remote>/<branch>?
- 40,656
- 60
- 209
- 315
-
1Related 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
/ – Andrew C Dec 19 '14 at 20:15` is almost never what you want to do
2 Answers
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/branchto look at the differences between your branch and the remote.git log remote/branch..HEADto look at changes with the remote.git branch -f branch remote/branchto throw out all your local changes.
- 153,029
- 25
- 195
- 336
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).