Skip to main content

Git checkout remote branch: how it works and when to use it

Written by:
blog-feature-snyk-container-custom-base-image-recommendations

December 15, 2020

0 mins read

Git 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.

wordpress-sync/image1-11

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 scan a local branch for security vulnerabilities?

Whenever you check out a remote branch on your local machine, you can scan it locally with the Snyk CLI. After signing up for a free account and installing the CLI tool, go to the root directory of the branch and use the following commands

  • snyk test for open-source dependency checking

  • snyk code test for statically analyzing your source code for vulnerabilities

  • snyk container test for analyzing your containers for security problems

  • snyk iac test for making sure your infrastructure as code is vulnerability free

Check out our handy cheat sheet and documentation for more information.

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.

How can I keep my default branch free of security vulnerabilities?

Snyk provides native integrations for all your projects on GitHub, Bitbucket, GitLab, and Azure Repos. By connecting your repository, you can utilize tools like Snyk Open Source, Snyk Code, Snyk IaC, and Snyk Container directly on your default branch. The git integration provides you with automatic fix pull requests and scans incoming PRs before you merge — allowing you to effortlessly merge security into your repository and prevent shipping vulnerable code.

blog-feature-snyk-container-custom-base-image-recommendations

Level Up Your CI/CD Pipelines

See how these 8 tips can help you catch security issues in the pipe BEFORE you push to production ⭐️