使用go-git
将特定本地分支推送到特定远程仓库的规范方式是什么?
我已经用go-git
检出并打开了一个本地仓库。
repo, err := git.PlainOpen("my-repo")
该仓库具有默认的origin
远程。
我正在尝试将此存储库的内容同步到另一个mirror
远程,因此我正在添加远程。
repo.CreateRemote(&config.RemoteConfig{
Name: "mirror",
URLs: []string{"git@github.com:foo/mirror.git"},
})
首先,我从origin
获取存储库内容
err = remote.Fetch(&git.FetchOptions{
RemoteName: "origin",
Tags: git.AllTags,
})
...使用remote.List()
进行所有感兴趣的分支和标签的发现。
最后一步是将分支推送到mirror
,同时根据映射重写分支名称。例如,检出为refs/heads/master
的refs/remotes/origin/master
应该被推送到mirror
远程作为main
。因此,我正在迭代分支并尝试逐个推送它们:
refSpec := config.RefSpec(fmt.Sprintf(
"+%s:refs/remotes/mirror/%s",
localBranch.Name().String(),
// map branch names, e.g. master -> main
mapBranch(remoteBranch.Name().Short()),
))
err = repo.Push(&git.PushOptions{
RemoteName: "mirror",
Force: true,
RefSpecs: []config.RefSpec{refSpec},
Atomic: true,
})
但这会导致
git.NoErrAlreadyUpToDate
,并且在 mirror
远程上没有任何动作发生。
cmd.Old == cmd.New
- 这些哈希值是相等的,因此推送命令不会被添加到队列中。 - David LukaclocalBranch
是指什么?你不想将refs/remotes/origin/*
推送到你的镜像吗? - LeGECgit-go
在本地进行检查并选择不发送看起来“最新”的内容是令人惊讶的。这实际上可能是一个问题。 - LeGEC