Git checkout remote branch: how it works and when to use it
December 15, 2020
0 mins readGit is a fantastic tool many developers use for version control on their projects. Although there are many other version control systems—like Subversion (SVN) and Concurrent Versioning System (CVS)—git is by far the most commonly used. A good reason for this is the focus on distributed development and the easy way to use branches. Let’s take a look at branches in git and what git checkout remote branch
actually means.
What is a branch?
A branch is a simple way to diverge from the main development pipeline. It is commonly used to develop a new feature or bugfix in a branch. This way, you encapsulate the changes and keep your main or master branch clean. Branches in git are very useful as they are effortless and relatively cheap to create. Unlike other version control systems, in git, a branch is not a copy of your code but merely a pointer to the original node where the branch originated.

How do I check out a branch?
If you already have a branch on your local machine, you can simply check out or switch to that branch using the command git checkout <branch name>
.
When you want to create a new branch from your main branch with the name “dev”, for example, use git branch dev
—this only creates the branch. If you want to work in this branch and commit to it, you need to check out this branch just like before using git checkout dev
.
Note: when you check out a branch on your local machine, all commits will be on the new branch and not on the main. Knowing this, you can also make a branch from a branch recursively. This might sound weird, but imagine you are creating a new feature in a new branch and you want to experiment a bit. It totally makes sense to do this in a separate level branch that originates from your feature branch.
How do I checkout a remote branch?
A remote branch is the best way to share your development work with other people in your team. It is good to mention that git checkout remote branch
is not an actual existing command. If you want to check out a remote branch someone published, you first have to use git fetch
. This command downloads the references from your remote repository to your local machine, including the reference to the remote branch. Now all you need to do is use git checkout <remote branch name>
.
How do I create a local branch from a remote branch?
After a fetch
, you can check out the remote branch as mentioned earlier. This means that there is a local copy of the branch available on your machine. If you would check out a remote branch but name it differently on your local machine you can run:
git checkout -b myLocalName origin/remoteName
Your local branch name, myLocalName
will be connected to the remote branch remoteName
. Note that origin
is the standard reference to the original remote repository my project was cloned from. This can be different, for instance, when you are working with multiple remotes.
How do I turn my local branch into a remote branch?
If you’re on a local branch myNewFeature
and want to share this branch remotely you have to set the upstream to make it a remote branch.
When I want to push my changes, first I have to use -u
or --set-upstream
like this:
git push -u origin myNewFeature
Now the local branch also has a remote counterpart. The next time I want to push changes I can just use git push
without any parameters.
Using Snyk with GitHub
Snyk's GitHub integration lets you:
Continuously perform security scanning across all the integrated repositories
Detect vulnerabilities in your open source components
Provide automated fixes and upgrades
Interested in learning more? Sign up for a free account.
Live Hack: Exploiting AI-Generated Code
Gain insights into best practices for utilizing generative AI coding tools securely in our upcoming live hacking session.