Reputation: 91693
How do I check out the remote test
branch? I can see it with git branch -r
. I tried:
git checkout test
, which does nothinggit checkout origin/test
gives * (no branch)
Upvotes: 8815
Views: 9276113
Reputation: 9084
I tried the above solution, but it didn't work. Try this, it works:
git fetch origin 'remote_branch':'local_branch_name'
This will fetch the remote branch and create a new local branch (if it doesn't exist already) with name local_branch_name
and track the remote one in it.
Upvotes: 428
Reputation: 4214
What I usually do in this case is:
Align local git repo to remote one
git fetch origin
create a new local branch test
tracking the remote branch test
(i.e. origin/test
)
git checkout -b test origin/test
test that the tracking is correct
git pull
it should say Already up-to-date.
Upvotes: 8
Reputation: 5094
Though there are already a lot of answers and the commands below are supposed to work:
git pull
or git fetch
git checkout <remote_branch>
But in my case, i was not able to get all the remote branches, though git pull or git fetch was not throwing any error, but when i ran git branch -a
, it was showing my local branches and one remote branch, which was not even right.
To fix that, i simply removed the remote:
git remote remove origin
then add the remote again:
git remote add origin <remote_url>
Then, run the above commands:
git pull
or git fetch
and then
git checkout <remote_branch>
Upvotes: 7
Reputation: 369
Please follow the command to create an empty folder. Enter that and use this command:
git clone your_project_url
cd iPhoneV1/
git checkout 1_4_0_content_discovery
example with git responses:
saifurs-Mini:YO-iOS saifurrahman$ git clone your_project_url
Cloning into 'iPhoneV1'...
remote: Counting objects: 34230, done.
remote: Compressing objects: 100% (24028/24028), done.
remote: Total 34230 (delta 22212), reused 15340 (delta 9324)
Receiving objects: 100% (34230/34230), 202.53 MiB | 294.00 KiB/s, done.
Resolving deltas: 100% (22212/22212), done.
Checking connectivity... done.
saifurs-Mini:YO-iOS saifurrahman$ cd iPhoneV1/
saifurs-Mini:iPhoneV1 saifurrahman$ git checkout 1_4_0_content_discovery
Branch 1_4_0_content_discovery set up to track remote branch 1_4_0_content_discovery from origin.
Switched to a new branch '1_4_0_content_discovery'
Upvotes: 3
Reputation: 24
What worked for me was using:
git pull origin main
git checkout <branch name>
git pull origin <branch name>
Upvotes: -1
Reputation: 2660
for those have tried all the answers but still not succeeded, you can try to use my powershell script
$branchName = 'RemoteBranchNameToClone'
git fetch origin "${branchName}:${branchName}"
git checkout $branchName
# Explicitly setup up the upstream to the right remote branch
git branch --set-upstream-to=$branchName $branchName
git config --get remote.origin.fetch
will return: +refs/heads/master:refs/remotes/origin/master
it is stored in the .git/config file
[remote "origin"]
url = https://abc-swt.visualstudio.com/abc/_git/efg
fetch = +refs/heads/master:refs/remotes/origin/master
which happens when clone the repo with the --single-branch or when do shadow clone:
git clone --depth 100
but does not explicitly set the--no-single-branch
switch, because the--depth
implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.
To save your life, do shadow clone like this:
git clone --no-single-branch --shallow-submodules --depth=1000 <repo_url>
orgit clone --no-single-branch --shallow-submodules --shallow-since=2023-08-08 <repo_url>
we need to change it with:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
then we can do it simply:
git fetch
git checkout branchName
Upvotes: 0
Reputation: 104870
You basically see the branch, but you don't have a local copy of that 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:
git fetch && git checkout test
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
:
Upvotes: 148
Reputation: 1893
I tried many answers here but still couldn't checkout to a remote branch, turns out that when I first cloned the repo, I had added --single-branch
flag which could be the reason why I was encountering the error when trying to create a new branch from a remote branch, to resolve this problem we can update the repository's configuration to fetch all branches from the remote repository:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch
git branch -r
and now we can continue with
git checkout -b feature/FS origin/feature/FS
Upvotes: 6
Reputation: 128059
The answer has been split depending on whether there is one remote repository configured or multiple. The reason for this is that for the single remote case, some of the commands can be simplified as there is less ambiguity.
Updated for Git 2.23: For older versions, see the section at the end.
In both cases, start by fetching from the remote repository to make sure you have all the latest changes downloaded.
$ git fetch
This will fetch all of the remote branches for you. You can 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 read only copies of the remote branches. To work on a branch you need to create a local branch from it. This is done with the Git command switch
(since Git 2.23) by giving it the name of the remote branch (minus the remote name):
$ git switch test
In this case Git is guessing (can be disabled with --no-guess
) that you are trying to checkout and track the remote branch with the same name.
In the case where multiple remote repositories exist, the remote repository needs to be explicitly named.
As before, start by fetching the latest remote changes:
$ git fetch origin
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
$ 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
git switch
was added in Git 2.23, prior to this git checkout
was used to switch branches.
To checkout out with only a single remote repository:
git checkout test
if there are multiple remote repositories configured then it becomes a bit longer
git checkout -b test <name of remote>/test
Upvotes: 11871
Reputation: 2599
I always do:
git fetch origin && git checkout --track origin/branch_name
Upvotes: 78
Reputation: 8267
There are many alternatives, for example:
Alternative 1:
git fetch && git checkout test
It's the simplest way.
Alternative 2:
git fetch
git checkout test
It's the same, but in two steps.
Upvotes: 26
Reputation: 416
TL;DR
Using git switch
rather than git checkout
. More details are on this page.
I think the answer is obsolete. Git split some functions of checkout
to switch
and restore
now.
The following is my summary:
If you want to update something for a remote branch, you should create a local branch to "track" the remote branch. You can update anything you want in local and finally push to remote. If you check out to the remote branch directly after cloning your repository, you may see the "detached HEAD" status and the following message from Git:
Note: switching to 'origin/asd'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at d3e1083 Update a
So how can we create a local branch to track a remote branch?
To create a local branch to track a remote branch, you can use git checkout <remote branch name>
or git switch <remote branch name>
. If you have a file or folder has same name as your remote branch name, git checkout
would output some error message, but git switch
can work normally!
Example:
See all branches, and we want to create a local branch to track the remote branch remotes/origin/asd
, and we also have the file name asd
:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/asd
remotes/origin/ereres
remotes/origin/master
remotes/origin/zxc
$ ls
a asd
The filename is same as remote branch, and Git should output some error messages if we are using the git checkout
command to create a local branch to track a remote branch
$ git checkout asd
fatal: 'asd' could be both a local file and a tracking branch.
Please use -- (and optionally --no-guess) to disambiguate
It works if we are using git switch
!
$ git switch ereres
Branch 'ereres' set up to track remote branch 'ereres' from 'origin'.
Switched to a new branch 'ereres'
$ git branch -vv
* ereres 3895036 [origin/ereres] Update a
master f9e24a9 [origin/master] Merge branch 'master' of
Upvotes: 17
Reputation: 1340
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:
git remote show origin
Use these steps to fetch remote branches:
git fetch <origin name> <remote branch name>:<local branch name>
git checkout <local branch name > (local branch name should the name that you given fetching)
Example:
git fetch origin test:test
git checkout test
Upvotes: 40
Reputation: 381
For us, it seems the remote.origin.fetch
configuration gave a problem. Therefore, we could not see any other remote branches than master
, so git fetch [--all]
did not help. Neither git checkout mybranch
nor git checkout -b mybranch --track origin/mybranch
did work, although it certainly was at remote.
The previous configuration only allowed master
to be fetched:
$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master
Fix it by using *
and fetch the new information from origin:
$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git fetch
...
* [new branch] ...
...
Now we could git checkout
the remote branch locally.
I don't have any idea how this configuration ended up in our local repository.
Upvotes: 14
Reputation: 2034
To get all remote branches, use this:
git fetch --all
Then check out to the branch:
git checkout test
Upvotes: 14
Reputation: 8258
For some reason, I couldn't do:
git checkout -b branch-name origin/branch-name
It was throwing the error:
fatal: 'origin/branch-name' is not a commit and a branch 'branch-name' cannot be created from it
I had to do:
git checkout -b branch-name commit-sha
Upvotes: 10
Reputation: 179
It seems to my that no one suggested the simplest way (or maybe I'm too dumb to think this is "a way"). But anyway, try this:
git pull origin remoteBranchName
git switch remoteBranchName
This worked for me in the same case (a branch created on the remote after my last pull request).
Upvotes: 14
Reputation: 1107
I am not sure. I keep doing this day in and day out. The following command works like gem.
dev being the branch you want to checkout.
git fetch && git checkout dev
Upvotes: 0
Reputation: 9
In your case, you can use this command:
git checkout -b test origin/test
Upvotes: -5
Reputation: 3643
Working Commands
git fetch origin 'remote_branch':'local_branch_name'
git switch 'local_branch_name'
git pull origin 'remote_branch':'local_branch_name'
The first one is for fetching the branch and creating a local branch from a remote branch.
The second one is for switching to the local branch.
The third is for pulling the latest changes of remote to the local branch.
Upvotes: 7
Reputation: 881
I used that one:
git clean -fxd # removes untracked (new added plus ignored files)
git fetch
git checkout {branchname}
git reset --hard origin/{branchname} # removes staged and working directory changes
Upvotes: 11
Reputation: 37865
In this case, you probably want to create a local test
branch which is tracking the remote test
branch:
$ git branch test origin/test
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.
To create the local branch and switch to it, use:
$ git checkout -b test origin/test
Upvotes: 819
Reputation: 323872
Sidenote: With modern Git (>= 1.6.6), you are able to use just
git checkout test
(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:
git checkout -b test HEAD
A more modern approach as suggested in the comments:
@Dennis:
git checkout <non-branch>
, for examplegit checkout origin/test
results in detached HEAD / unnamed branch, whilegit checkout test
orgit checkout -b test origin/test
results in local branchtest
(with remote-tracking branchorigin/test
as upstream) – Jakub Narębski Jan 9 '14 at 8:17
emphasis on git checkout origin/test
Upvotes: 1552
Reputation: 1406
I was stuck in a situation seeing error: pathspec 'desired-branch' did not match any file(s) known to git.
for all of the suggestions above. I'm on Git version 1.8.3.1.
So this worked for me:
git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD
The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:
git fetch origin desired-branch
From github.com:MYTEAM/my-repo
* branch desired-branch -> FETCH_HEAD
Upvotes: 51
Reputation: 2469
If the remote branch name begins with special characters you need to use single quotes around it in the checkout command, or else Git won't know which branch you are talking about.
For example, I tried to checkout a remote branch named #9773
, but the command didn't work properly, as shown in the picture below:
For some reason, I wondered if the sharp symbol (#) could have something to do with it, and then I tried surrounding the branch name with single quotes, like '#9773'
rather than just #9773
, and fortunately it worked fine.
git checkout -b '#9773' origin/'#9773'
Upvotes: 7
Reputation: 1140
None of these answers worked for me. This worked:
git checkout -b feature/branch remotes/origin/feature/branch
Upvotes: 26
Reputation: 215
git fetch --all
would fetch all the remote branches to your local
git checkout test
would switch you to the test branch
Upvotes: 7
Reputation: 962
Just run these two commands and you should be good to go.
git checkout <branch-name>
git pull <remote> <branch-name>
Upvotes: 7
Reputation: 11264
git checkout -b "Branch_name" [ B means Create local branch]
git branch --all
git checkout -b "Your Branch name"
git branch
git pull origin "Your Branch name"
successfully checkout from the master branch to dev branch
Upvotes: 14
Reputation: 16568
To clone a Git repository, do:
git clone <either ssh url /http url>
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:
git checkout -t origin/future_branch (for example)
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:
git checkout -t -b enhancement origin/future_branch
Now your local branch name is enhancement
, but your remote branch name is future_branch
.
Upvotes: 80