我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
Git通常(如果未指定)从一个或多个其他存储库中获取所有分支和/或标记(refs,请参见:Git-ls-refs)以及完成其历史所需的对象。换句话说,它获取已经下载的对象可以访问的对象。看:git fetch真正做什么?
有时,您可能有一些分支/标记没有直接连接到当前的分支/标记,因此git pull-all/git fetch-all在这种情况下不会有帮助,但您可以通过以下方式列出它们:
git ls-remote -h -t origin
并通过知道引用名称手动获取它们。
因此,要获取所有信息,请尝试:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
如果您对存储库进行了浅处理,--depth=10000参数可能会有所帮助。
然后再次检查所有分支:
git branch -avv
如果上述方法无效,则需要手动将丢失的分支添加到跟踪列表中(因为它们不知何故丢失了):
$ git remote -v show origin
...
Remote branches:
master tracked
通过git远程设置分支,如:
git remote set-branches --add origin missing_branch
因此在获取后,它可能出现在remotes/origin下:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
故障排除
如果您仍然无法获得主分支以外的任何内容,请检查以下内容:
仔细检查遥控器(gitremote-v),例如。验证git-config-branch.master.remote是源代码。通过:git remote show origin检查origin是否指向正确的URL(参见本文)。
其他回答
我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。
这对我有用:
假设您需要在本地重新创建分支:
git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name
因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:
$ git checkout -b mynewbranch
$ git branch -a
master
remotes/sjp/master
remotes/sjp/mynewbranch
$ git fetch (habit to always do before)
$ git rebase remotes/sjp/mynewbranch
下面是另一个简短的单行命令为所有远程分支创建本地分支:
(git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout
如果已经创建了跟踪本地分支,它也可以正常工作。您可以在第一个git克隆之后或以后的任何时间调用它。
如果克隆后不需要签出主分支,请使用
git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout
所有分支机构
从Git项目下载所有分支的脚本
安装:
sudo git clone https://github.com/marceloviana/allBranches.git && sudo cp -rfv allBranches/allBranches.sh /usr/bin/allBranches && sudo chmod +x /usr/bin/allBranches && sudo rm -rf allBranches
准备好的现在只需调用命令(allBranches)并告诉Git项目目录,您希望下载所有分支
Use
示例1:
~$ allBranches /var/www/myproject1/
示例2:
~$ allBranches /var/www/myproject2/
示例3(如果已经在项目目录中):
~$ allBranches ./
or
~$ allBranches .
查看结果:
git branch
参考:
存储库allBranches GitHub:https://github.com/marceloviana/allBranches
如果您有许多远程分支要同时获取,请执行以下操作:
git pull --all
现在,您可以根据需要签出任何分支,而无需访问远程存储库。
注意:这不会创建任何未签出分支的工作副本,这就是问题所在。对此,请参见
大鱼的回答戴夫的回答
我编写了这些小PowerShell函数,以便能够签出我的所有Git分支,这些分支位于源远程。
Function git-GetAllRemoteBranches {
iex "git branch -r" <# get all remote branches #> `
| % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> `
| % { Out-Null; $matches['name'] } <# write does names #>
}
Function git-CheckoutAllBranches {
git-GetAllRemoteBranches `
| % { iex "git checkout $_" } <# execute ' git checkout <branch>' #>
}
更多的Git函数可以在我的Git设置库中找到。
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?