What is the canonical way to push specific single local branch to specific remote with go-git
?
I have a local repository checked out and opened with go-git
repo, err := git.PlainOpen("my-repo")
The repo has default origin
remote.
I'm trying to sync contents of this repository to another remote mirror
, so I'm adding the remote
repo.CreateRemote(&config.RemoteConfig{
Name: "mirror",
URLs: []string{"git@github.com:foo/mirror.git"},
})
First I fetch repo contents from origin
err = remote.Fetch(&git.FetchOptions{
RemoteName: "origin",
Tags: git.AllTags,
})
... and do a discovery of all branches and tags of interest with remote.List()
Final step is pushing the branches to mirror
, while rewriting branch names based on a map. E.g. refs/remotes/origin/master
checked out as refs/heads/master
should be pushed to mirror
remote as main
. Therefore I'm iterating over the branches and trying to push them one by one:
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,
})
But this results in git.NoErrAlreadyUpToDate
and there's nothing happening on the mirror
remote.