Question

I've just started learning Git, and the tutorial I'm using says that when cloning remote repositories, switching to a remote branch is a big no-no. That is - if I've cloned a repository that has a branch names branch1, and I want to work on it, I need to use

git checkout --track -b branch1 origin/branch1

to track it. However, when I tried to use

git checkout branch1

Git replied

Branch branch1 set up to track remote branch branch1 from origin.
Switched to a new branch 'branch1'

So, my common sense tells my that since that tutorial was written, Git's developers fixed that pitfall and made it so if someone tries to switch to a remote branch, Git automatically does it the right way and creates a local branch that tracks it. However, I can't find any reference to this change in the release notes or in a Google search.

So, did they fix it? Is it now safe to simply checkout those remote branches? Or maybe I misunderstood the warning in that tutorial, and it was referring to another pitfall I should watch out from?

Was it helpful?

Solution

You aren't checking out a remote branch.

Checking out branch1 isn't the same as checking out the "remote branch". The following command attempts to check out a local branch which doesn't exist:

git checkout branch1

while the following command checks out the commit pointed to by the remote branch:

git checkout origin/branch1

which results in a detached head, possibly the pitfall the tutorial was warning about.

Git will automatically create a local branch and set it up to track a remote of the same name if you attempt to check out a branch which doesn't exist locally, but which has a branch of the same name on a remote.

OTHER TIPS

From Documentation/RelNotes/1.6.6.txt:

  • "git checkout frotz" when there is no local branch "frotz" but there is only one remote tracking branch "frotz" is taken as a request to start the named branch at the corresponding remote tracking branch.

So yes, Git developers simplified the process a bit: you don't have to specify --track -b any longer to create a local remote-tracking branch. IIRC, before this change Git used to complain about a missing local branch "frotz".

Like meagar says, checking out remote branches is still possible by using the git checkout origin/branch1 syntax: it's OK to do this if you just want to look around, but you are not supposed to make permanent changes to them. Here's the warning that Git prints out when you check out a remote branch:

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at a48aafd... Merge branch 'maint'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top