Appearance
How to Checkout a Remote Branch in Git
During the process of working on a shared git repository, coworkers might need access to one another’s branches. This can be done using the git checkout command. Usually, there exists a single remote. However, there can be cases when a developer has to work with branches in multiple remotes.
Here, we will discuss both of the scenarios: checking out a single remote and checking out multiple remote branches.
How to Checkout to a Branch in a Single Remote
Here are the steps you should take to checkout a single remote branch:
Fetching a Remote
The first step is fetching a remote branch by using the git fetch command, like this:
Fetch remote branches
bash
git fetchDisplaying Branches
The second step is displaying the branches to choose, which one you want to checkout by acting as follows:
List all branches
bash
git branch -v -aChecking out a Remote Branch
The final step is using the git checkout command in the following way:
Checkout a remote branch
bash
git checkout -b test origin/testNote: Starting with Git 2.23,
git switchis recommended for branch navigation. The equivalent command isgit switch -c test origin/test.
How to Checkout to a Branch in Multiple Remotes
Now, let’s figure out how to switch to multiple remote branches accurately. You can easily do that by following the steps below.
Fetching Remote Branches
In this case, your first step should be using the git fetch command with the --all option to fetch all the remotes, like this:
Fetch all remotes
bash
git fetch --allDisplaying Branches
You can see the branches that are available for checkout by calling the git branch command:
List available branches
bash
git branch -v -aChecking out Branches
And, finally, to checkout a branch in multiple remotes, you should use the following command:
Create and checkout a new branch
bash
git checkout -b test <name of remote>/testOr the shorthand version:
Checkout with tracking shorthand
bash
git checkout -t <name of remote>/testThe git branch Command
The git branch command is targeted at creating, listing and deleting branches. It doesn’t give you an option to switch between branches and put a forked history back together. Most version control systems allow branching. It is aimed at pointing to a snapshot of your changes. For summarizing the changes whenever you intend to fix the bugs or add new properties is created a new branch.
The git fetch Command
The git fetch command is applied for downloading commits, references, and files from the remote repository into a local one. With it, you can see what other members of the group have been working on. The content that has been fetched, should be accurately checked out using the git checkout command.
The git checkout Command
Switching branches and restoring working tree files is what the git checkout command is used for. You can run it on commits, branches, as well as, files. It is especially useful for switching between several features in a single repository.