Switching Branches in Git
Here are a few examples to show you how to list local and remote branches and switch between those branches.
Working on a huge project with multiple contributors requires managing multiple branches in Git.
That means that as the features/branches grow, you will have to manage more and more branches in the future.
git checkout <BRANCH-NAME>
You may also use the switch option:
git switch <BRANCH-NAME>
The difference between git switch and checkout is that the checkout command is capable of doing a little more than just switching branches.
Let's see about switching branches in more detail with examples.
Local and remote branches
Switching from one branch to another branch is only present if the branch is present locally. So, lets take a look at how to get a list of remote and local branches.
Get a list of locally available branches
When you intend to switch from one branch to another existing branch, make sure that the intended branch exists locally.
To get a list of all locally available branches, use the following command:
$ git branch * master switching-branch
As of now, I only have two branches available locally. They are
Get a list of remote branches
Local branches don't always tell the truest story. So, to see the branches that the remote repository has, run the following command.
$ git branch -r origin/HEAD -> origin/master origin/new-remote-only-branch origin/master
As you might have noticed, the local branch
switching-branch is not in upstream, and the remote branch
new-remote-only-branch is also not available locally.
This can be a problem when you try to switch to
Switching local branches
Now that you know how to differentiate between a local and a remote branch, let us dive into how to switch from one branch to another.
When you already have a local branch, simply use the 'git checkout' command with the following syntax:
git checkout <BRANCH-NAME>
This will change your branch from whatever branch you were in earlier, to a branch that you specified.
Switching remote branches
In case you want to do this for a remote branch, all you have to do is run the
git fetch command first and then checkout.
git fetch --all git checkout <REMOTE-BRANCH-NAME>
git fetch command is similar to
git pull in terms of functionality. The only difference is that
git pull will download the remote content for the currently active local branch and then, it will execute
git merge as well.
Let's look at an example:
$ git fetch --all Fetching origin $ git branch * master switching-branch $ git checkout new-remote-only-branch Branch 'new-remote-only-branch' set up to track remote branch 'new-remote-only-branch' from 'origin'. Switched to a new branch 'new-remote-only-branch' $ git branch master * new-remote-only-branch switching-branch
new-remote-only-branch was not present locally. And we did not clone it explicitly. And yet, we were able to switch to the
new-remote-only-branch as if it were a local branch.
How cool is that?!
This article covers how you can differentiate between a remote and a local branch (for the purposes of being able to checkout said branch), how to switch to a local branch and how to switch to a remote branch without explicitly cloning it.