How to Checkout a Remote Git Branch

Assuming you occasionally work on a side project every evening after work versioned by Git and saved to a remote repository such as GitHub, Bitbucket or GitLab.

And on one glorious day at work, your lunch break was extended by an hour or two. In order not to waste the time, you decided to continue working on your side project.

To download the source code to the computer you use at work, you cloned the repo like so:

git clone https://github.com/username/side-project.git

When a git clone is done, only the master branch gets downloaded.

And if you were currently working on a new feature in a separate Git branch, that branch and every other branch are not pulled to your local repository folder.

If you are in (or find yourself) in a similar situation, the guide below will come in handy.

Solution

First off, fetch all the remote branches (and tags) using git fetch --all

Finally, checkout the remote branch like so:

git checkout --track origin/branch-name

If the branch doesn’t exist locally and the branch name matches the exact name on remote, Git will pull the files from the remote branch and create a tracking branch for you when you checkout like so:

git checkout branch-name

I hope you find this piece useful.