I have cloned a Git repository and I would like to switch to a particular branch from the following list (e.g., branch1, branch2, branch3).
Should I have to say git fetch origin branch2?
How do I know that I have switched to this particular branch on my local repository?
How do I switch to say ‘branch3’ from ‘branch2’?
Recent versions of git have enough DWIM (“Do What I Mean!”) logic to understand you just doing:
… in that situtation. That will work if there is no local branch called
branch1, and there’s only one remote-tracking branch that ends withbranch1– in that case it’s equivalent to the longer:… which should work in every circumstance.
I realize that I missed answering some of your later questions. git stores the state of the branches from the
originrepository in so-called “remote-tracking branches” – these are the ones that look likeorigin/master,origin/branch1,origin/branch2, etc. If you want to update all of these fromoriginjust use:To see all of your remote-tracking branches, you can use:
The meaning of the full command I quoted above:
… is:
branch1(thecheckout -b branch1bit)origin/branch1(i.e. base it on the lastest cached state ofbranch1fromorigin)origin/branch1as being “upstream” ofbranch1in your git config (the--trackbit, although that’s actually implied if the start point is a remote-tracking branch)