Checking Out a Remote Branch in Git

During the process of working on a shared git repository, coworkers might need access to one another’s repositories. You can solve this problem with the help of the git checkout command.

Your first step should be fetching all the remote branches, using the git fetch command, like this:

git fetch all

You can see the branches, that are available for checkout, calling the git branch command:

git branch -v -a

With the help of the new versions of Git, you can check out your remote branch as a local one. You can do it by running:

git checkout <remote_branch>

In the older Git versions you had to create a new branch, based on the remote branch, like this:

git checkout <remote_branch> origin/<remote_branch>

Afterward, checkout a new local branch resetting it to the remote branches last commit. For doing that is used the git reset command:

git checkout -b <branchname>

git reset --hard origin/<branchname>

There you are.

Definition of the 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.

Definition of 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.

Definition of 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.

Definition of the git reset Command

The git reset command is a useful method for undoing changes in Git. It includes three invocation forms matching the tree internal state management systems of Git. Those systems are called three trees of Git. They include the HEAD, your staging index and, finally, the working directory.

Do you find this helpful?

Related articles