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?
You aren’t checking out a remote branch.
Checking out
branch1isn’t the same as checking out the “remote branch”. The following command attempts to check out a local branch which doesn’t exist:while the following command checks out the commit pointed to by the remote branch:
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.