The remote contains various branches such as origin/daves_branch
:
$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master
How do I checkout daves_branch
locally so that it tracks origin/daves_branch
? I tried:
$ git fetch origin discover
$ git checkout discover
All of the information written below was accurate, but a new command, git switch
has been added that simplifies the effort.
If daves_branch
exists on the remote repository, but not on your local branch, you can simply type:
git switch daves_branch
Since you do not have the branch locally, this will automatically make switch
look on the remote repo. It will then also automatically set up remote branch tracking.
Note that if daves_branch
doesn't exist locally you'll need to git fetch
first before using switch
.
You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch , tracking the remote branch origin/daves_branch . When you push your changes the remote branch will be updated.
For most recent versions of Git:
git checkout --track origin/daves_branch
--track
is shorthand for git checkout -b [branch] [remotename]/[branch]
where [remotename] is origin in this case and [branch] is twice the same, daves_branch in this case.
For Git 1.5.6.5 you needed this:
git checkout --track -b daves_branch origin/daves_branch
For Git 1.7.2.3 and higher, this is enough (it might have started earlier, but this is the earliest confirmation I could find quickly):
git checkout daves_branch
Note that with recent Git versions, this command will not create a local branch and will put you in a 'detached HEAD' state. If you want a local branch, use the --track
option.
Full details are here: 3.5 Git Branching - Remote Branches, Tracking Branches
I have usedfetch
followed by checkout
...
git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>
...where <rbranch>
is the remote branch or source ref and <lbranch>
is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under options in the explanation of <refspec>
.
Bash is so smart it auto completes the first command if I tab after the first few letters of the remote branch. That is, I don't even have to name the local branch; Bash automatically copies the name of the remote branch for me. Thanks, Bash!
Also as the answer in this similar Stack Overflow post shows, if you don't name the local branch in fetch
, you can still create it when you check it out by using the -b
flag. That is, git fetch <remote> <branch>
followed by git checkout -b <branch> <remote>/<branch>
does exactly the same as my initial answer. And evidently, if your repository has only one remote, then you can just do git checkout <branch>
after fetch
and it will create a local branch for you. For example, you just cloned a repository and want to check out additional branches from the remote.
I believe that some of the documentation for fetch
may have been copied verbatim from pull
. In particular the section on <refspec>
in options is the same. However, I do not believe that fetch
will evermerge
, so that if you leave the destination side of the colon empty, fetch
should do nothing .
NOTE: git fetch <remote> <refspec>
is short for git fetch <remote> <refspec>:
which would therefore do nothing, but git fetch <remote> <tag>
is the same as git fetch <remote> <tag>:<tag>
which should copy the remote <tag>
locally.
I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise, I now would use the accepted answer , which is explained in detail in the first section of the checkout description and later in the options section under the explanation of --track
, since it's a one-liner. Well... sort of a one-liner, because you would still have to run git fetch <remote>
first.
FYI: The order of the <refspecs>
(source:destination) explains the bizarre pre Git 1.7 method for deleting remote branches . That is, push nothing into the destination refspec.
If you are trying to "checkout" a new remote branch (that exists only on the remote, but not locally), here's what you'll need:
git fetch origin
git checkout --track origin/<remote_branch_name>
This assumes you want to fetch from origin . If not, replace origin by your remote name.
To checkout myBranch that exists remotely and not a locally - This worked for me:
git fetch --all
git checkout myBranch
I got this message:
Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'
fetch the branch locally:
git fetch origin <branchName>
move to that branch:
git checkout <branchName>
Use git branch -a
(both local and remote branches) or git branch -r
(only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branch
to the remote and create a local branch.
There is also a git-ls-remote command to see all the refs and tags for that remote.
The title and the question are confused:
If the question is, how can I get a remote branch to work with, or how can I Git checkout a remote branch? , a simpler solution is:
With Git (>= 1.6.6) you are able to use:
git checkout <branch_name>
If local <branch_name>
is not found, but there does exist a tracking branch in exactly one remote with a matching name, treat it as equivalent to:
git checkout -b <branch_name> --track <remote>/<branch_name>
See documentation for Git checkout
For your friend:
$ git checkout discover
Branch discover set up to track remote branch discover
Switched to a new branch 'discover'
To fetch a branch that exists on remote, the simplest way is:
git fetch origin branchName
git checkout branchName
You can see if it already exists on remote with:
git branch -r
This will fetch the remote branch to your local and will automatically track the remote one.
Use:
git checkout -b serverfix origin/serverfix
This is a common enough operation that Git provides the --track
shorthand:
git checkout --track origin/serverfix
In fact, this is so common that there's even a shortcut for that shortcut. If the branch name you're trying to checkout (a) doesn't exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:
git checkout serverfix
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
git checkout -b sf origin/serverfix
Now, your local branch sf
will automatically pull from origin/serverfix
.
Source: Pro Git, 2nd Edition , written by Scott Chacon and Ben Straub (cut for readability)
使用这个简单的命令:
git checkout -b 'your_branch' origin/'remote branch'
There are many alternatives, and my favourites are:
- Alternative 1:
git fetch --all
git checkout YourBranch
Using this alternative using a branch that exist remotely, but not in your local.
- Alternative 2:
git checkout -b 'YourBranch' origin/'YourRemote'
Probably, this is the simplest way.
What helped me was
1) To view all available remote branches (eg 'remote-branch-name')
git branch -r
2) Create a local branch using remote branch name
git fetch && git checkout 'remote-branch-name'
git fetch
git branch -r
git checkout <branch_name>
您也可以一次性获取并签出远程分支:
git fetch && git checkout the-branch-name
I typed
git checkout <branch_name>
and got
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Switched to a new branch '<branch_name>'
At times you are asked not to fiddle with the master branch and work only the remote branch (as I was asked to). So all you need is the remote branch.
So to clone the remote branch alone (without the master), do this
git clone url --branch remote_branch_name
where, remote_branch_name is the name of the remote branch
For example,
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15
This will make sure that you clone the remote branch to your local branch with the name of the remote branch.
Now if you commit your code and push, the code will be submitted to that branch alone.
The steps are as follows;
git fetch origin
or git fetch --all
, this will fetch all the remote branches to your local and then this the second option you can proced with.
git checkout --track origin/<The_remote_branch you want to switch over>
Then work on this branch and you can verify whether you are on that branch or not by typing
git branch
It displayes the branch you currently in.
只需尝试:
git pull origin your_branch_name
Let's say that your remote is git@xyz.git and you want its random_branch branch. The process should be as follows:
First check the list of your remotes by
git remote -v
If you don't have the git@xyz.git remote in the above command's output, you would add it by
git remote add xyz git@xyz.git
git fetch xyz
git checkout -b my_copy_random_branch xyz/random_branch
git branch -a
The local branch my_copy_random_branch would be tracking the random_branch branch of your remote.
如果您想获取所有远程分支,请输入:
git fetch --all
If you have a repository that was cloned with --depth 1
then many of the commands that were listed will not work. For example, see here
% git clone --depth 1 https://github.com/repo/code
Cloning into 'code'...
cd code
remote: Counting objects: 1778, done.
remote: Compressing objects: 100% (1105/1105), done.
remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0
Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done.
Checking out files: 100% (1215/1215), done.
% cd code
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
% git fetch origin other_branch
remote: Counting objects: 47289, done.
remote: Compressing objects: 100% (15906/15906), done.
remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0
Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done.
Resolving deltas: 100% (30151/30151), completed with 362 local objects.
From https://github.com/repo/code
* branch other_branch-> FETCH_HEAD
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
%
In this case I would reclone the repository, but perhaps there are other techniques eg git shallow clone (clone --depth) misses remote branches
git fetch --all & git checkout <branch name>
git fetch && git checkout <your friend's branch name>
应该可以解决问题
I want to give you one-liner command for fetching all the remote branches to your local and switch to your desired newly created local branch:
git fetch && git checkout discover
After running the above command you will get the below message:
Switched to a new branch 'discover'
Branch discover set up to track remote branch discover from origin.
The first line states that switched to a new branch - why new? It is already there in remote!
But actually you have to create it locally too. The branch is taken from the remote index and created locally for you.
Here discover
is a new branch which were created from your repository's remote branch discover
.
But the second line gives more information than the first one which tell us that:
Our branch is set up to track remote branch with the same name.
Although git fetch
fetches all branches to local. But if you run git branch
after it, you will see only master
branch in local. Why ?
Because for every branch you have in remote you have to create it locally too, for tracking it as git checkout <branchname>
as we have done in the above example.
After running git checkout
command you can run git branch
, and now you can see both the branch:
Check your .git/config
file, particularly what tracking is present on fetch for that remote.
[remote "randomRemote"]
url = git@github.com:someUser/someRepo.git
fetch = +refs/heads/*:refs/remotes/randomRemote/*
If it has heads/*
pointing to randomRemote/*
, when you run git fetch randomRemote
, it will fetch all branches.
Then you can just checkout that branch.
Otherwise,
You need to add remote branches to the tracking using this. Check your .git/config
after running this. You will understand.
git remote set-branches --add randomRemote randomBranch
Run git fetch randomRemote
. This will fetch the remote branch.
Now you can run git checkout randomBranch
.
If you already know your remote branch like so...
git remote
=> One
=> Two
and you know the branch name you wish to checkout, for example, br1.2.3.4 , then do
git fetch One
=> returns all meta data of remote, that is, the branch name in question.
All that is left is to checkout the branch
git checkout br.1.2.3.4
Then make any new branches off of it.
git branch <name> --track origin/<name>
You use 'git pull' to keep your branches separate. I will use the actual repository and branch names to help since 'lbranch' and 'rbranch' are tough to decipher.
Let's use:
You, or any colleague, can run this to pull only your branch, no matter how many branches there are:
git init git pull git@myteam.unfuddle.com:myteam/tlc daves_branch:refs/remotes/origin/daves_branch
一个简单的命令git checkout remote_branch_name
将帮助您创建一个本地分支,其中包含远程分支中的所有更改。
If you download the repository with git clone <repo_url> -b <branch>
(only cloning certaing branch), you should modify the <repo_name>/.git/config
file. Replace or modify the line that references the fetch target of the [remote "origin"]
section to let the command git fetch --all
discover all branches:
[remote "origin"]
url = <repo_git_url>
fetch = +refs/heads/master:refs/remotes/origin/master
Be sure to set the fetch parameter point to /heads/master
.
Care with git fetch --all
because this will fetch all, so may take a long time.
git checkout -b branch_name
git pull remote_name branch_name
获取和签出本地不存在的远程分支的最简单方法是:
git fetch && git checkout remote_branch
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.