With new version of Git 2.23 (git --version
), you can use git switch
as an alternative to git checkout
as well.
To checkout a remote branch. (After git fetch, you can use git branch -a
to view all branch name)
git fetch && git switch <branch_name>
If you have more than one remote, you can use
git fetch --all && git switch <branch_name>
Note: git fetch --all
is equivalent to git remote update
Also, as pointed here in Eugene's answer above, if multiple remotes with same branch name exist then above command will not work. In that case, you can use below command with remote_name along with -c
which creates a local copy of branch as well.
git fetch -all && git switch -c <branch_name> <remote_name>/<branch_name>
Benefit of git switch
is that it removes ambiguity by only targeting branch whereas git checkout
can be used for both file name and branch .Like git checkout test
can mean that test
can be either a file name or branch and if you have file name as test
and you type that to checkout a branch test
thenif there is no test
branch,it will reset contest of test
file. git can get confused in somecases and will not produce desirable output as mentioned in oldman's answer above.