我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
git克隆应该复制整个存储库。尝试克隆它,然后运行gitbranch-a。它应该列出所有分支。如果您想切换到分支“foo”而不是“master”,请使用git checkout foo。
其他回答
当您执行“gitclone”时git://location“,将获取所有分支和标记。
为了在特定远程分支上工作,假设它是源远程分支:
git checkout -b branch origin/branchname
使用别名。虽然没有任何原生Git单行程序,但您可以将自己的定义为
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
然后将其用作
git clone-branches
从本地回购中克隆不能与gitclone和gitfetch一起使用:许多分支/标签将保持不匹配。
获取包含所有分支和标记的克隆。
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
要获取包含所有分支和标记但也包含工作副本的克隆,请执行以下操作:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
以下是实现此目的的最佳方法:
mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard
此时,您已经拥有了远程存储库及其所有分支的完整副本(使用git分支进行验证)。如果远程存储库有自己的远程设备,则可以使用--mirror而不是--bare。
下面是根据前面的答案改编的跨平台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,它总是让您在尝试时感到困扰。
推荐文章
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖