How to checkout remote branches with Git
This guide will help you understand how to efficiently work with remote branches using Git, covering everything from checking out a remote branch for the first time to dealing with branches from different remotes.
1. Basic concepts
Remote branch: A branch hosted on a remote repository (like GitHub). You interact with it to share your work and collaborate with others.
Local branch: A branch in your local repository where you make and commit changes, stored directly on your machine.
2. Setting up your environment
First, ensure you have Git installed on your machine. Open your terminal (Linux/Mac) or Git Bash (Windows) to execute Git commands.
3. Cloning a repository (if needed)
If you haven't already cloned the remote repository to your local machine, do so by running:
git clone <repository-url>
For more info on git clone
see: the difference between cloning a git repo with SSH and HTTPS.4. Fetching remote branches
Before you checkout a remote branch, make sure your local Git is aware of any existing remote branches:
git fetch origin
This command fetches updated data from the remote named origin
without merging changes into your local branches.
5. Checking out a remote branch
First Time Checkout:
If you're checking out a remote branch for the first time, use:
git checkout -b <branch-name> origin/<branch-name>
This command creates a new local branch that tracks the remote branch.
Checkout and track a remote branch:
To checkout an existing remote branch and set your local branch to track it:
git checkout --track origin/<branch-name>
Git automatically names your local branch the same as the remote one.
Checking out with a new local branch name:
If you want to give the local branch a different name:
git checkout -b <new-local-branch-name> origin/<branch-name>
6. Fetch and checkout in one step
You can fetch and checkout a remote branch in one step using:
git fetch origin <branch-name>:<local-branch-name>git checkout <local-branch-name>
7. Dealing with branches from different remotes
If you're working with multiple remotes, you may need to checkout branches from different sources. When checking out a branch on a different remote, you can specify the remote name with:
git checkout -b <local-branch-name> <remote-name>/<branch-name>
8. Pulling changes from the remote branch
After checking out a remote branch, in order to update your local branch with the latest upstream changes, run:
git pull
This command fetches changes from the remote and merges them into your local branch.
9. Creating a new branch from a remote branch
To create a new branch based on the current state of a remote branch, run:
git checkout -b <new-branch-name> origin/<existing-branch-name>
10. Upstream branches
Set upstream on push: When pushing a new local branch for the first time, run:
git push -u origin <branch-name>
This sets the remote branch as upstream for your local branch.
Checkout and set upstream together:
For an existing branch, you can set the upstream during checkout:
git checkout --set-upstream-to=origin/<branch-name> <local-branch-name>
Common issues and resolutions
Remote branch not found: Make sure you've run
git fetch
to update your list of remote branches.Branch name conflicts: If there's a naming conflict ie. a branch already exists with that same name in the same repo, select a different name for your local branch. See: renaming git branches for more info.
Permissions: Ensure you have the necessary permissions to access the remote repository and its branches.
For further reading see the official git documentation on remote branches.