3

When i try to use worktree checkout; it doesn't work and the code returns error

err = worktree.Checkout(&git.CheckoutOptions{
    Create: true,
    Branch: "main",
})
if err != nil {
    log.Fatal("Cannot create 'main' branch\n" + err.Error())
}

Output:

[FATA] Cannot create 'main' branch
reference not found

I get the same error when i use

repository.CreateBranch(&config.Branch{
    Name: "main",
})
capsci
  • 65
  • 1
  • 5

1 Answers1

0

While it is true GitHub will soon rename master to main, the default branch for a Git repository (on GitHub or not) is still master for now.

So try first:

err = worktree.Checkout(&git.CheckoutOptions{
    Create: true,
    Branch: "master",
})

But if the goal is to create a new branch, make sure the repo is either initialized or already checked out to a valid branch.
For example, see repository_test.go

    r, _ := Init(memory.NewStorage(), nil)
    testBranch := &config.Branch{
        Name:   "foo",
        Remote: "origin",
        Merge:  "refs/heads/foo",
    }
    err := r.CreateBranch(testBranch)

The OP capsci adds in the comments:

I tried removing Merge during branch create, but got "reference not found" error when checking out (w/ and w/o Create option)

I ended up using hack in hairyhenderson/gomplate PR 1217:

h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main")) 
err = storer.SetReference(h); 

these 2 lines after git.Init(storer, fs), and I was good to go.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC, but above did not help. The new commits i make still go on 'master' branch – capsci Mar 02 '22 at 20:07
  • @capsci Your original question (18 months ago) was talking about an error, not about commits going to master. Anyway, once you have created the branch (using the code in my answer), you still need to checkout it first, *then* create commits: they will go on the new branch (because of the checkout) – VonC Mar 02 '22 at 22:55
  • Hi @VonC, sorry for updating this after 18 months – capsci Mar 04 '22 at 18:55
  • Hi @VonC, I should I've explained the problem better. Basically, I was trying to checkout a new "main" branch on an empty repo. `repo, err := git.Init(memory.NewStorage(), memfs.New())` `err = repo.CreateBranch(&config.Branch{Name:”main”,Merge: "refs/heads/foo"})` `worktree,err := repo.Worktree()` `err = worktree.Checkout(&git.CheckoutOptions{Branch: “main”})` I still get "reference not found" error when I Checkout newly created branch (with or without 'Create' set in CheckoutOptions). Can you please let me know what else am I missing? – capsci Mar 04 '22 at 19:15
  • @capsci For testing, I would avoid the Merge "refs/heads/foo" part: when you just initialized a repository, I am sure that particular reference ("foo" branch) would not be found. – VonC Mar 04 '22 at 19:21
  • Hi @VonC, thanks for comment. I tried removing Merge during branch create, but got "reference not found" error when checking out (w/ and w/o Create option) I ended up using hack in https://github.com/hairyhenderson/gomplate/pull/1217 `h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main"))` `err = storer.SetReference(h)`; these 2 lines after `git.Init(storer, fs)` and I was good to go Thanks again for your help and pointers :) – capsci Mar 09 '22 at 14:56
  • FYI, I also tried committing to default branch(master) and then creating and checking-out "main". But this resulted both "master" and "main" branches when I performed `remote.Push()`. In a follow up attempt; I wondered if I can delete "master" branch from my local in-memory "git init" before performing Push; but `*Repository.Branches` listed both "master" and "main"; but `*Repository.Config.Branches`(which is used by `*Repository.DeleteBranch`) did not have "master" for some reason. I might be connecting too many dots here. But just sharing what I saw. – capsci Mar 09 '22 at 15:00
  • @capsci Thank you for your feedback. I have included your comment/option in the answer for more visibility. – VonC Mar 09 '22 at 15:45