What is the canonical way to push specific single local branch to specific remote with 100?

我签出了一个本地存储库,并以go-git英镑打开

repo, err := git.PlainOpen("my-repo")

回购的默认设置为origin Remote.

我正在try 将此存储库的内容同步到另一个遥控器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/remotes/origin/master AS refs/heads/master应推送到mirror远程AS main.因此,我迭代遍历分支,并try 逐个推送它们:

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遥控器上什么也没有发生.

推荐答案

当将单个分支推送到遥控器时,refSpec应该是格式+refs/heads/localBranchName:refs/remotes/remoteName/remoteBranchName,例如here:

// RefSpec is a mapping from local branches to remote references.
...
// eg.: "+refs/heads/*:refs/remotes/origin/*"
//
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
type RefSpec string

但作为

"+refs/heads/localBranchName:refs/heads/remoteBranchName"

取而代之的是.请参见example:

    refSpecStr := fmt.Sprintf(
        "+%s:refs/heads/%s",
        localBranch.Name().String(),
        mapBranch(remoteBranch.Name().Short()),
    )
    refSpec := config.RefSpec(refSpecStr)
    log.Infof("Pushing %s", refSpec)
    err = repo.Push(&git.PushOptions{
        RemoteName: "mirror",
        Force:      true,
        RefSpecs:   []config.RefSpec{refSpec},
        Atomic:     true,
    })

Go相关问答推荐

困扰围棋官方巡回赛的S建议所有方法都使用同一类型的接收器

如何防止程序B存档/删除围棋中程序A当前打开的文件?

无法在32位计算机上运行Golang应用程序

重新赋值变量时未清除动态类型-这是错误吗?

在Go中旋转矩阵

go-chi: 接受带有反斜杠的 url 路径参数

Go 中的sync.Cond 与 Wait 方法

在 Windows 11 上运行 go mod tidy 时的 gitlab 权限问题

Global Thread-local Storage 在 Go 中的可行性和最佳实践

Go 中的 YAML 自定义标签

甚至用天真的洗牌分配?

regex.ReplaceAll 但如果替换则添加相同数量的字符

读取非UTF8编码的文件内容并正确打印出来

如何在 helm 中将字符串连接到 .AsConfig 的结果?

级联调用泛型函数时的泛型类型推断

Golang泛型在用作 map 元素时不起作用

如何在Go中替换符号并使下一个字母大写

如何在 Golang 中使用具有相同名称或特定关键字的行或列重新排列/排序 CSV

将未知长度切片的值分配给Go中的 struct ?

为什么 go-cmp Equal() 说 struct 不是完全相等的,即使所有字段都非常相等?