我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
关于
git checkout-b实验来源/实验
使用
git checkout -t origin/experimental
或者更冗长,但更容易记住
git checkout --track origin/experimental
在跟踪远程存储库方面可能更好。
其他回答
当您执行“gitclone”时git://location“,将获取所有分支和标记。
为了在特定远程分支上工作,假设它是源远程分支:
git checkout -b branch origin/branchname
以下是实现此目的的最佳方法:
mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard
此时,您已经拥有了远程存储库及其所有分支的完整副本(使用git分支进行验证)。如果远程存储库有自己的远程设备,则可以使用--mirror而不是--bare。
gitclone--原始repo上的镜像很好地实现了这一点。
git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin
除了用户没有人走在正确的轨道上之外,这些答案都无法解决问题。
我在将存储库从一个服务器/系统移动到另一个服务器或系统时遇到了问题。当我克隆存储库时,它只为master创建了一个本地分支,所以当我推送到新的远程时,只推送了master分支。
所以我发现这两种方法非常有用。
方法1:
git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
方法2:
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
我编写了这些小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平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖