我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
您可以轻松切换到分支,而无需使用花哨的“gitcheckout-b somebranch origin/somebranch”语法。您可以执行以下操作:
git checkout somebranch
Git将自动执行正确的操作:
$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'
Git将检查在一个远程中是否存在同名的分支,如果存在,它将以与您明确指定的远程分支相同的方式对其进行跟踪。从git 1.8.2.1的git checkout手册页面:
如果未找到<branch>,但中确实存在跟踪分支只有一个具有匹配名称的远程(称为<remote>),视为相当于$git checkout-b<branch>--track<remote>/<branch>
其他回答
只需执行以下操作:
$ git clone git://example.com/myproject
$ cd myproject
$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'
$ git pull
Already up-to-date.
$ git branch
* branchxyz
master
$ git branch -a
* branchxyz
master
remotes/origin/HEAD -> origin/master
remotes/origin/branchxyz
remotes/origin/branch123
你看,gitclonegit://example.com/myprojectt获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。
截至2017年初,该评论中的答案有效:
gitfetch<origin name><branch name>为您关闭分支。虽然这不会同时拉动所有分支,但您可以对每个分支单独执行此操作。
如何为远程源匹配模式上的每个分支创建本地分支。
#!/bin/sh
git fetch --all
git for-each-ref --format='%(refname:short)' refs/remotes/origin/pattern |\
sed 's@\(origin/\)\(.*\)@\2\t\1\2@' |\
xargs -n 2 git branch --track
获取所有远程引用(分支/标记),然后创建本地引用。应该可以在大多数系统上快速运行,而无需查看索引或依靠抨击。
这个解决方案使我能够将一个存储库“复制”到另一个:
git merge path/to/source.git --mirror
cd source.git
git remote remove origin
git remote add origin path/to/target.git
git push origin --all
git push origin --tags
在目标存储库中,我可以看到与原始存储库相同的分支和标记。
下面是根据前面的答案改编的跨平台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迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖