How do I check out a remote Git branch

0 votes

I assume that someone has pushed a branch called test with git push origin test to a shared repository for which I can see the branch with git branch -r. I have been trying to check out the remote test branch by trying:-

  • The git checkout test which does nothing

  • The git checkout origin/test gives * (no branch), which is confusing me as to how it is possible to be on "no branch"?

How do I check out a remote Git branch?

Feb 10, 2022 in Others by Soham
• 9,710 points
755 views

1 answer to this question.

0 votes

The answer to your question has been divided depending on whether there is one remote repository configured or various of them which are configured. This would be because the single remote case along with some of the commands can be simplified as there is less ambiguity.

Updated for Git 2.23: For older versions check the end section


With a single or multiple remotes, start by fetching from the remote repository to make sure you have all the latest changes updated and downloaded.
 

$ git fetch

This will help you in fetching all of the remote branches for you and you will be able to see the branches available for checkout with:
 

$ git branch -v -a ... remotes/origin/test

The branches that start with remotes/* can be thought of as reading only copies of the remote branches and to work on a branch you need to create a local branch from it which is done with the Git command switch (since Git 2.23) by giving it the name of the remote branch excluding the name of the remote as mentioned below:
 

$ git switch test

In this case Git is guessing that you are trying to checkout and track the remote branch with the same name. This can be can be disabled with --no-guess

However, in the case where multiple remote repositories exist, the remote repository needs to be explicitly named. To incorporate those changes, use:-

$ git fetch origin

Which is going to help you get all of the remote branches for you and with the help of the line below, can see the branches available for checkout:
 

$ git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in with -c to create a new local branch:
 

$ git switch -c test origin/test

For more information about using git switch:
 

$ man git-switch

The image below shows how the fetch is different to pull and the way it operates:- image

Please note that the git switch was added in Git 2.23, prior to this git checkout was used to switch branches. In order to checkout out with only a single remote repository, please use the line below:
 

git checkout test

However, if there there are multiple remote repositories configured it becomes a bit longer for which use:-
 

git checkout -b test <name of remote>/test

answered Feb 10, 2022 by Rahul
• 9,680 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How do I carry out functional testing?

Well, functional testing is pretty simple. it ...READ MORE

answered Feb 18, 2019 in Others by Jobin
677 views
0 votes
1 answer

How do I start studying Node.JS to create a restful API?

Node.js is an open source server environment ...READ MORE

answered Jun 11, 2019 in Others by ArchanaNagur
• 2,360 points
914 views
0 votes
1 answer

How do I get a cheap domain name? - web hosting

Many web hosting companies like amazon's lighsail ...READ MORE

answered Oct 28, 2019 in Others by Yesha
804 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,925 views
0 votes
1 answer

How to checkout Hadoop 2.6.0 from git

Clone the following Git repository: git clone git ...READ MORE

answered Apr 23, 2018 in Big Data Hadoop by kurt_cobain
• 9,390 points
841 views
0 votes
1 answer

Where do I find a list of all mat-icons -- Angular

To answer your question, the prior Material.io ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
6,972 views
0 votes
1 answer

Lucky patcher, how can I protect from it?

public void checkSignature(final Context context) { try ...READ MORE

answered Feb 8, 2022 in Others by Rahul
• 9,680 points
923 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP