如何在 Git 中克隆所有远程分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/67699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clone all remote branches in Git?
提问by Peter Coulton
I have a master
and a development
branch, both pushed to GitHub. I've clone
d, pull
ed, and fetch
ed, but I remain unable to get anything other than the master
branch back.
我有一个master
和一个development
分支,都推送到GitHub。我已经clone
d、pull
ed 和fetch
ed,但我仍然无法得到除master
分支以外的任何东西。
I'm sure I'm missing something obvious, but I have read the manual and I'm getting no joy at all.
我确定我遗漏了一些明显的东西,但我已经阅读了手册,但我一点也不高兴。
回答by emk
First, clone a remote Gitrepository and cdinto it:
$ git clone git://example.com/myproject
$ cd myproject
Next, look at the local branches in your repository:
接下来,查看存储库中的本地分支:
$ git branch
* master
But there are other branches hiding in your repository! You can see these using the -a
flag:
但是还有其他分支隐藏在您的存储库中!您可以使用-a
标志查看这些:
$ git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
If you just want to take a quick peek at an upstream branch, you can check it out directly:
如果您只是想快速浏览一下上游分支,可以直接查看:
$ git checkout origin/experimental
But if you want to work on that branch, you'll need to create a local tracking branch which is done automatically by:
但是如果你想在那个分支上工作,你需要创建一个本地跟踪分支,它通过以下方式自动完成:
$ git checkout experimental
and you will see
你会看到
Branch experimental set up to track remote branch experimental from origin.
Switched to a new branch 'experimental'
That last line throws some people: "New branch" - huh? What it really means is that the branch is taken from the index and created locally for you. The previousline is actually more informative as it tells you that the branch is being set up to track the remote branch, which usually means the origin/branch_name branch
最后一行抛出了一些人:“新分支” - 嗯?它的真正含义是分支取自索引并在本地为您创建。在前面的行实际上是更多的信息,因为它告诉你的分支正在建立跟踪远程分支,这通常意味着起源/ branch_name分支
Now, if you look at your local branches, this is what you'll see:
现在,如果您查看本地分支机构,您会看到以下内容:
$ git branch
* experimental
master
You can actually track more than one remote repository using git remote
.
实际上,您可以使用git remote
.
$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
remotes/win32/master
remotes/win32/new-widgets
At this point, things are getting pretty crazy, so run gitk
to see what's going on:
在这一点上,事情变得非常疯狂,所以运行gitk
看看发生了什么:
$ gitk --all &
回答by Gabe Kopley
If you have many remote branches that you want to fetch at once, do:
如果您想一次获取许多远程分支,请执行以下操作:
$ git pull --all
Now you can checkout any branch as you need to, without hitting the remote repository.
现在,您可以根据需要检出任何分支,而无需访问远程存储库。
回答by bigfish
This Bashscript helped me out:
这个Bash脚本帮助了我:
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a
它将为所有远程分支创建跟踪分支,除了 master (您可能从原始 clone 命令获得)。我想你可能还需要做一个
git fetch --all
git pull --all
to be sure.
为了确定。
One liner:
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
As usual: test in your setup before copying rm -rf universe as we know itCredits for one-liner go to user cfi
一个班轮:
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
像往常一样:在复制我们所知的 rm -rf Universe 之前在您的设置中进行测试单线积分转至用户 cfi
回答by Dave
Using the --mirror
option seems to copy the remote
tracking branches properly.
However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.
使用该--mirror
选项似乎可以remote
正确复制跟踪分支。但是,它将存储库设置为裸存储库,因此您必须在之后将其转换回普通存储库。
git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch
Reference: Git FAQ: How do I clone a repository with all remotely tracked branches?
回答by Nikos C.
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax. You can just do:
您可以轻松切换到分支,而无需使用花哨的“git checkout -b somebranch origin/somebranch”语法。你可以这样做:
git checkout somebranch
Git will automatically do the right thing:
Git 会自动做正确的事情:
$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'
Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 1.8.2.1:
Git 将检查同名分支是否存在于一个远程分支中,如果存在,它会像您明确指定它是远程分支一样跟踪它。从 Git 1.8.2.1 的 git-checkout 手册页:
If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to
$ git checkout -b <branch> --track <remote>/<branch>
如果 <branch> 未找到,但在一个具有匹配名称的远程(称为 <remote>)中确实存在跟踪分支,则视为等效于
$ git checkout -b <branch> --track <remote>/<branch>
回答by murphytalk
Regarding,
关于,
$ git checkout -b experimental origin/experimental
$ git checkout -b 实验源/实验
using
使用
$ git checkout -t origin/experimental
or the more verbose but easier to remember
或者更冗长但更容易记住
$ git checkout --track origin/experimental
might be better, in terms of tracking a remote repository.
在跟踪远程存储库方面可能会更好。
回答by Luuk Paulussen
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.
您正在执行的提取应该获取所有远程分支,但不会为它们创建本地分支。如果您使用 gitk,您应该会看到描述为“remotes/origin/dev”或类似内容的远程分支。
To create a local branch based on a remote branch, do something like:
要基于远程分支创建本地分支,请执行以下操作:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:
应该返回如下内容:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
现在,当您在 dev 分支上时,“git pull”会将您的本地 dev 更新到与远程 dev 分支相同的点。请注意,它将获取所有分支,但只会将您所在的分支拉到树的顶部。
回答by elmarco
When you do "git clone git://location", all branches and tags are fetched.
当你执行“git clone git://location”时,所有的分支和标签都会被获取。
In order to work on top of a specific remote branch, assuming it's the origin remote:
为了在特定远程分支之上工作,假设它是原始远程分支:
git checkout -b branch origin/branchname
回答by nobody
Use aliases. Though there aren't any native Git one-liners, you can define your own as
使用别名。虽然没有任何本地 Git 单行程序,但您可以将自己定义为
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
and then use it as
然后将其用作
git clone-branches
回答by Cerran
Why you only see "master"
为什么你只看到“主人”
git clone
downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master". By default, git branch
only shows local branches, which is why you only see "master".
git clone
下载所有远程分支,但仍将它们视为“远程”,即使这些文件位于您的新存储库中。对此有一个例外,即克隆过程会从名为“master”的远程分支创建一个名为“master”的本地分支。默认情况下,git branch
只显示本地分支,这就是为什么你只能看到“master”。
git branch -a
shows all branches, including remote branches.
git branch -a
显示所有分支,包括远程分支。
How to get local branches
如何获得当地分支机构
If you actually want to work on a branch, you'll probably want a "local" version of it. To simply create local branches from remote branches (without checking them out and thereby changing the contents of your working directory), you can do that like this:
如果你真的想在一个分支上工作,你可能需要它的“本地”版本。要简单地从远程分支创建本地分支(不检查它们并因此更改工作目录的内容),您可以这样做:
git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree
In this example, branchone
is the name of a local branch you're creating based on origin/branchone
; if you instead want to create local branches with different names, you can do this:
在这个例子中,branchone
是你正在创建的本地分支的名称origin/branchone
;如果你想用不同的名字创建本地分支,你可以这样做:
git branch localbranchname origin/branchone
Once you've created a local branch, you can see it with git branch
(remember, you don't need -a
to see local branches).
一旦你创建了一个本地分支,你就可以看到它git branch
(记住,你不需要-a
看到本地分支)。