Skip to content

Repository.CreateBranch fails if a remote branch exists with the same name #85

@mykter

Description

@mykter

I tried to create a local branch based on a remote branch by doing this:

err = repo.CreateBranch(&gitconfig.Branch{Name: b, Remote: "origin", Merge: plumbing.NewBranchReferenceName(b)})

However it returns ErrBranchExists even though the local branch doesn't exist. It looks like it is comparing based on name alone, not whether it is local or remote.

I instead got it to work following _example/branch:

targetRef, err := repo.Reference(plumbing.NewRemoteReferenceName("origin", b), true)
if err != nil {
	t.Fatalf("Couldn't get reference for remote branch: %v", err)
}
ref := plumbing.NewHashReference(plumbing.ReferenceName("refs/heads/"+b), targetRef.Hash())
err = repo.Storer.SetReference(ref)

Have I misunderstood something, or is it a bug that CreateBranch doesn't differentiate between local and remote branches?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleIssues/PRs that are marked for closure due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions