DEV Community

DEVEV
DEVEV

Posted on

Git) Checkout Remote Branch

When to checkout git remote branch?

When the remote branch already existed, and you want to pull the branch and its changes to your local environment.


How to checkout git remote branch?

It depends on whether there is one configured remote repository or multiple repositories.

With Single Remote Repository

1. Fetch all remote branches
Start by fetching all the latest changes from remote repository
$ git fetch

# alternatives(depends on your remote name)
$ git fetch origin 
$ git fetch upsteam
Enter fullscreen mode Exit fullscreen mode
2. Check all branches available for checkout
-v(verbose): show extra info
-a(all): list all
$ git branch -v -a
Enter fullscreen mode Exit fullscreen mode
3. Checkout to the remote branch
Now you can checkout and track the remote branch
$ git switch <branch name>
Enter fullscreen mode Exit fullscreen mode

With Multiple Remote Repositories

1. Fetch all remote branches
$ git fetch

# alternatives(depends on your remote name)
$ git fetch origin 
$ git fetch upsteam
Enter fullscreen mode Exit fullscreen mode
2. Check all branches available for checkout
$ git branch -v -a
Enter fullscreen mode Exit fullscreen mode
3. Checkout to the remote branch
In case where multiple remotes exist, the remote repositories should be explicitly named.
Checkout the branch with -c option to create new local branch
$ git switch -c <branch name> origin/<branch name>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)