我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
当前回答
TL;DR答案
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
(似乎pull会从所有遥控器获取所有分支,但我总是先获取,以确保安全。)
仅当服务器上存在本地分支未跟踪的远程分支时,才运行第一个命令。
完整答案
您可以像这样从所有遥控器获取所有分支:
git fetch --all
这基本上是一个有力的动作。
fetch更新远程分支的本地副本,因此这对本地分支总是安全的,但是:
fetch不会更新本地分支(跟踪远程分支);如果要更新本地分支,仍然需要拉动每个分支。fetch不会创建本地分支(跟踪远程分支),您必须手动执行此操作。如果要列出所有远程分支:查看所有的分支
要更新跟踪远程分支的本地分支,请执行以下操作:
git pull --all
然而,这仍然不够。它只适用于跟踪远程分支的本地分支。要跟踪所有远程分支,请在git pull-all之前执行以下oneliner:
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
P.S.AFAIK git fetch--all和git远程更新是等效的。
Kamil Szot的评论,人们发现这很有用。
我必须使用:对于“gitbranch-r”中的remote;do git branch--跟踪${remote#origin/}$remote;完成因为您的代码创建了名为origin/branchname和每当我参考它。
其他回答
我相信您已经通过以下方式克隆了存储库:
git clone https://github.com/pathOfrepository
现在使用cd转到该文件夹:
cd pathOfrepository
如果键入gitstatus,您可以看到所有:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
要查看所有隐藏的分支类型,请执行以下操作:
git branch -a
它将列出所有远程分支。
现在,如果您想在任何特定分支上签出,只需键入:
git checkout -b localBranchName origin/RemteBranchName
要列出远程分支,请执行以下操作:
git branch -r
要将远程分支签出为本地分支,请执行以下操作:
git checkout -b local_branch_name origin/remote_branch_name
git remote add origin https://yourBitbucketLink
git fetch origin
git checkout -b yourNewLocalBranchName origin/requiredRemoteBranch (use tab :D)
现在本地您的NewLocalBranchName是您所需的RemoteBranch。
只有这三个命令才能获得所有分支:
git clone --mirror repo.git .git (gets just .git - bare repository)
git config --bool core.bare false
git reset --hard
使用git fetch和git checkout RemoteBranchName。
这对我来说很好。。。