0

I don't know how to set the HEAD reference for a bare repository.

Throw the BareRepositoryException with message

Additional information: An error was raised by libgit2. Category = Repository (BareRepo).

when I trying repo.Checkout(repo.Branches["balabala"])

Aimeast
  • 1,609
  • 14
  • 29
  • This question is about when you are using `libgit2sharp`. For anyone coming here looking for how to set `HEAD` for a bare repository on the command-line or on a git hosting service, have a look at https://stackoverflow.com/a/3302018/260122 and https://stackoverflow.com/a/2962737/260122. – clacke Mar 28 '17 at 16:38

2 Answers2

1

Checking out will indeed update update the HEAD, but also update the current content of the workdir with the content of Commit being checked out.

This indeed doesn't work with a bare repository as there's no working directory.

The correct way to move the HEAD in a bare repository is to update the target of this reference:

  • repo.Refs.UpdateTarget(repo.Refs.Head, repo.Refs["myBranch"]) will update the HEAD to make it point to the branch mybranch while keeping it attached

  • repo.Refs.UpdateTarget(repo.Refs.Head, repo.Refs["myBranch"].Tip.Id) will update the HEAD to make it directly to the commit at the tip of the branch, thus detaching the HEAD

nulltoken
  • 64,429
  • 20
  • 138
  • 130
0

fine, I found repo.Refs.UpdateTarget("HEAD", repo.Branches["master"].Tip.Sha), it's working.

Aimeast
  • 1,609
  • 14
  • 29