如何获取所有 Git 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10312521/
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 fetch all Git branches
提问by David542
I cloned a Git repository, which contains about five branches. However, when I do git branch
I only see one of them:
我克隆了一个 Git 存储库,其中包含大约五个分支。但是,当我这样做时,git branch
我只看到其中之一:
$ git branch
* master
I know that I can do git branch -a
to see allthe branches, but how would I pull all the branches locally so when I do git branch
, it shows the following?
我知道我可以git branch -a
看到所有分支,但是我如何在本地拉动所有分支,所以当我这样做时git branch
,它会显示以下内容?
$ git branch
* master
* staging
* etc...
回答by Wookie88
You can fetch all branches from all remotes like this:
您可以像这样从所有遥控器获取所有分支:
git fetch --all
It's basically a power move.
这基本上是一个权力的举动。
fetch
updates local copies of remote branches so this is always safe for your local branches BUT:
fetch
更新远程分支的本地副本,因此这对您的本地分支始终是安全的,但是:
fetch
will not updatelocal branches (which trackremote branches); if you want to update your local branches you still need to pull every branch.fetch
will not createlocal branches (which trackremote branches), you have to do this manually. If you want to list all remote branches:git branch -a
fetch
不会更新本地分支(跟踪远程分支);如果你想更新你的本地分支,你仍然需要拉每个分支。fetch
不会创建本地分支(跟踪远程分支),您必须手动执行此操作。如果要列出所有远程分支:git branch -a
To updatelocal branches which track remote branches:
要更新跟踪远程分支的本地分支:
git pull --all
However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFOREgit pull --all
:
然而,这仍然是不够的。它仅适用于跟踪远程分支的本地分支。要跟踪所有远程分支,请执行此 oneliner之前git pull --all
:
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
TL;DR version
TL;DR 版本
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
(It seems that pull fetches all branches from all remotes, but I always fetch first just to be sure.)
(似乎 pull 从所有遥控器中获取所有分支,但为了确定起见,我总是先获取。)
Run the first command only if there are remote branches on the server that aren't tracked by your local branches.
仅当服务器上存在本地分支未跟踪的远程分支时才运行第一个命令。
P.S. AFAIK git fetch --all
and git remote update
are equivalent.
PS AFAIKgit fetch --all
和git remote update
是等效的。
Kamil Szot's comment, which folks have found useful.
Kamil Szot 的评论,人们发现它很有用。
I had to use:
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
because your code created local branches named
origin/branchname
and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.
我不得不使用:
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
因为您的代码创建了命名的本地分支,
origin/branchname
并且每当我提到它时,我都会收到“refname 'origin/branchname' 是不明确的。
回答by Learath2
To list remote branches:git branch -r
列出远程分支:git branch -r
You can check them out as local branches with:git checkout -b LocalName origin/remotebranchname
您可以通过以下方式将它们作为本地分支机构签出:git checkout -b LocalName origin/remotebranchname
回答by Michael Renner
You will need to create local branches tracking remote branches.
您将需要创建跟踪远程分支的本地分支。
Assuming that you've got only one remote called origin
, this snippet will create local branches for all remote tracking ones:
假设您只有一个名为 的远程origin
,此代码段将为所有远程跟踪创建本地分支:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
After that, git fetch --all
will update all local copies of remote branches.
之后,git fetch --all
将更新远程分支的所有本地副本。
Also, git pull --all
will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.
此外,git pull --all
将更新您的本地跟踪分支,但根据您的本地提交以及“合并”配置选项的设置方式,它可能会创建合并提交、快进或失败。
回答by GoZoner
If you do:
如果你这样做:
git fetch origin
then they will be all there locally. If you then perform:
那么他们都会在当地。如果你然后执行:
git branch -a
you'll see them listed as remotes/origin/branch-name. Since they are there locally you can do whatever you please with them. For example:
你会看到它们被列为遥控器/来源/分支名称。因为他们就在当地,你可以对他们做任何你想做的事情。例如:
git diff origin/branch-name
or
或者
git merge origin/branch-name
or
或者
git checkout -b some-branch origin/branch-name
回答by Johnno Nolan
$ git remote update
$ git pull --all
This assumes all branches are tracked.
这假设所有分支都被跟踪。
If they aren't you can fire this in Bash:
如果不是,您可以在 Bash 中触发它:
for remote in `git branch -r `; do git branch --track $remote; done
Then run the command.
然后运行命令。
回答by Tim Lum
The Bash for
loop wasn't working for me, but this did exactly what I wanted. All the branches from my origin mirrored as the same name locally.
Bashfor
循环对我不起作用,但这正是我想要的。来自我的起源的所有分支在本地镜像为相同的名称。
git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'
See Mike DuPont's comment below. I think I was trying to do this on a Jenkins Server which leaves it in detached head mode.
请参阅下面的Mike DuPont的评论。我想我试图在 Jenkins 服务器上执行此操作,这使其处于分离头模式。
回答by Regis Zaleman
Use git fetch && git checkout RemoteBranchName
.
使用git fetch && git checkout RemoteBranchName
.
It works very well for me...
它对我来说非常有效......
回答by FedericoCapaldo
When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command
当您克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。随着命令
$ git branch -a
you can show all the branches of the repository, and with the command
您可以显示存储库的所有分支,并使用命令
$ git checkout -b branchname origin/branchname
you can then "download" them manually one at a time.
然后,您可以一次一个地手动“下载”它们。
However, there is a much cleaner and quicker way, though it's a bit complicated. You need three steps to accomplish this:
然而,有一种更干净、更快捷的方法,虽然它有点复杂。您需要三个步骤来完成此操作:
First step
create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:
$ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder $ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.
Second step
switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:
$ git config --bool core.bare false
Third Step
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.
$ git reset --hard
第一步
在您的机器上创建一个新的空文件夹并从存储库克隆 .git 文件夹的镜像副本:
$ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder $ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
文件夹 my_repo_folder 内的本地存储库仍然是空的,现在只有一个隐藏的 .git 文件夹,您可以从终端使用“ls -alt”命令查看。
第二步
通过将 git 配置的布尔值“bare”切换为 false,将此存储库从空(裸)存储库切换到常规存储库:
$ git config --bool core.bare false
第三步
抓取当前文件夹中的所有内容并在本地机器上创建所有分支,从而使其成为一个普通的 repo。
$ git reset --hard
So now you can just type the command git branch
and you can see that all the branches are downloaded.
所以现在您只需键入命令git branch
,您就可以看到所有分支都已下载。
This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.
这是您可以一次克隆包含所有分支的 git 存储库的快速方法,但这并不是您想以这种方式为每个项目做的事情。
回答by kenorb
You can fetch all the branches by:
您可以通过以下方式获取所有分支:
git fetch --all
or:
或者:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.
--depth=10000
如果您浅化了存储库,该参数可能会有所帮助。
To pull all the branches, use:
要拉出所有分支,请使用:
git pull --all
If above won't work, then precede the above command with:
如果以上不起作用,则在上述命令之前使用:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
as the remote.origin.fetch
could support only a specific branch while fetching, especially when you cloned your repo with --single-branch
. Check this by: git config remote.origin.fetch
.
因为remote.origin.fetch
在获取时只能支持特定的分支,尤其是当您使用--single-branch
. 通过以下方式检查:git config remote.origin.fetch
。
After that you should be able to checkout any branch.
之后,您应该可以结帐任何分支。
See also:
也可以看看:
To push all the branches to the remote, use:
要将所有分支推送到远程,请使用:
git push --all
eventually --mirror
to mirror all refs.
最终--mirror
反映所有参考。
If your goal is to duplicate a repository, see: Duplicating a repositoryarticle at GitHub.
如果您的目标是复制存储库,请参阅:在 GitHub 上复制存储库文章。
回答by marioosh
I usually use nothing else but commands like this:
我通常只使用这样的命令:
git fetch origin
git checkout --track origin/remote-branch
A little shorter version:
一个更短的版本:
git fetch origin
git checkout -t origin/remote-branch