How do I check out a remote Git branch?


Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r.



Now I'm trying to check out the remote test branch.



I've tried:



git checkout test which does nothing



git checkout origin/test gives * (no branch). Which is confusing. How can I be on "no branch"?



How do I check out a remote Git branch?



Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, you can just do:



(User masukomi points out below that git checkout test will NOT work in modern git if you have multiple remotes. In this case use git checkout -b test <name of remote>/test).



Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.



To fetch a branch, you simply need to:



This will fetch all of the remote branches for you. You can see the branches available for checkout with:



With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:



Sidenote: With modern Git (>= 1.6.6), you are able to use just



(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.



The * (no branch) in git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:



In this case, you probably want to create a local test branch which is tracking the remote test branch:



In earlier versions of git, you needed an explicit --track option, but that is the default now when you are branching off a remote branch.



While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:



fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?



If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:



As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.



I tried the above solution, but it didn't work. Try this, it works:



This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name and track the remote one in it.



This will DWIM for a remote not named origin (documentation):



To add a new remote, you will need to do the following first:



The first tells Git the remote exists, the second gets the commits.



Use:



Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.



To clone a Git repository, do:



The above command checks out all of the branches, but only the master branch will be initialized. If you want to checkout the other branches, do:



This command checks out the remote branch, and your local branch name will be same as the remote branch.



If you want to override your local branch name on checkout:



Now your local branch name is enhancement, but your remote branch name is future_branch.



Documentation



OK, the answer is easy... You basically see the branch, but you don't have a local copy yet...



You need to fetch the branch...



You can simply fetch and then checkout to the branch, use the one line command below to do that:



I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:



git fetch



You can try



or



First, you need to do:



git fetch # If you don't know about branch name



Second, you can check out remote branch into your local by:



-b will create new branch in specified name from your selected remote branch.



Commands



are equal to



and then



Both will create a latest fixes_for_dev from development



If the branch is on something other than the origin remote I like to do the following:



This will checkout the next branch on the upstream remote in to a local branch called second/next. Which means if you already have a local branch named next it will not conflict.



I use the following command:



git fetch && git checkout your-branch-name



git branch -r says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:



And then try checking out your remote branch again.



This worked for me.



I believe git fetch pulls in all remote branches, which is not what the original poster wanted.



The git remote show <origin name> command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.



Example:



Use these steps to fetch remote branches:



Example:



You can start tracking all remote branches with the following Bash script:



Here is also a single-line version:



Other guys and gals give the solutions, but maybe I can tell you why.



git checkout test which does nothing



Does nothing doesn't equal doesn't work, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?



If the answer is 'yes', I can tell you the cause.



The cause is that there is a file (or folder) named 'test' in your work tree.



When git checkout xxx parsed,



Please follow the command to create an empty folder. Enter that and use this command:



Fetch origin and checkout the branch.



To see the currently selected branch



To get newly created branches



To switch into another branch




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令