我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
对于使用Visual Studio代码的开发人员来说,一个更好的替代解决方案是使用Git Shadow Extension。
此Visual Studio代码扩展允许克隆存储库内容和目录,这些内容和目录可以通过分支名称或提交哈希进行筛选。这样,分支或提交可以用作新项目的样板/模板。
其他回答
当您执行“gitclone”时git://location“,将获取所有分支和标记。
为了在特定远程分支上工作,假设它是源远程分支:
git checkout -b branch origin/branchname
在这里,我为您编写了一个很好的函数,使其易于重复
gitCloneAllBranches() { # clone all git branches at once easily and cd in
# clone as "bare repo"
git clone --mirror $1
# rename without .git extension
with_extension=$(basename $1)
without_extension=$(echo $with_extension | sed 's/.git//')
mv $with_extension $without_extension
cd $without_extension
# change from "bare repository" to not
git config --bool core.bare false
# check if still bare repository if so
if [[ $(git rev-parse --is-bare-repository) == false ]]; then
echo "ready to go"
else
echo "WARNING: STILL BARE GIT REPOSITORY"
fi
# EXAMPLES:
# gitCloneAllBranches https://github.com/something/something.git
}
关于
git checkout-b实验来源/实验
使用
git checkout -t origin/experimental
或者更冗长,但更容易记住
git checkout --track origin/experimental
在跟踪远程存储库方面可能更好。
下面是根据前面的答案改编的跨平台PowerShell 7函数。
function Invoke-GitCloneAll($url) {
$repo = $url.Split('/')[-1].Replace('.git', '')
$repo_d = Join-Path $pwd $repo
if (Test-Path $repo_d) {
Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
} else {
Write-Host "`nCloning all branches of $repo..."
git -c fetch.prune=false clone $url -q --progress &&
git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
Write-Host "" #newline
}
}
注意:-c fetch.sprune=false使其包含通常会被排除的过时分支。如果你对它不感兴趣,就去掉它。
通过从函数中删除&&,可以在PowerShell 5.1(Windows 10中的默认值)中实现这一点,但这使得它即使在上一个命令失败时也会尝试git pull。因此,我强烈建议您只使用跨平台PowerShell,它总是让您在尝试时感到困扰。
这是一个使用awk的答案。如果在新的回购协议中使用,这种方法就足够了。
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
现有的分支将被简单地签出,或者声明为已经在其中,但是可以添加过滤器以避免冲突。
还可以修改它,以便调用显式git checkout-b<branch>-t<remote>/<branch>命令。
这个答案符合尼科斯·C的想法。
或者,我们可以指定远程分支。这是基于murphytalk的答案。
git branch -r | awk '{ system("git checkout -t " $NF) }'
它会对冲突发出致命的错误消息,但我认为它们是无害的。
这两个命令都可以别名。
使用nobody的答案作为参考,我们可以使用以下命令来创建别名:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
就我个人而言,我会使用跟踪所有分支或跟踪所有分支。
推荐文章
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域