0

I set up a remote called 'origin' and clone the remote into the local directory with checkout set to false so I can fetch the remote tags. I can list the remote tags but I can't find any way to fetch the contents.

If I call clone with checkout set to true, I get the HEAD which is great but I'd like to be able to roll back to a specific tagged version and can't see how.

user1016736
  • 390
  • 4
  • 7

1 Answers1

3

You can checkout any arbitrary commit, including a tag (for instance: 'lw'):

string commitPointer = refs/tags/lw;
var commit = repo.Lookup<Commit>(commitPointer);
Branch detachedHead = repo.Checkout(commitPointer);

You will be in a detached head though:

Assert.Equal("(no branch)", detachedHead.CanonicalName);
Assert.False(master.IsCurrentRepositoryHead);
Assert.True(repo.Info.IsHeadDetached);

But that HEAD does reference your tag:

Assert.True(detachedHead.IsCurrentRepositoryHead);
Assert.Equal(repo.Head, detachedHead);
Assert.Equal(commit.Sha, detachedHead.Tip.Sha);
Assert.True(repo.Head.IsCurrentRepositoryHead);
Assert.False(repo.Index.RetrieveStatus().IsDirty);

Assert.False(detachedHead.IsRemote);
Assert.Equal(detachedHead.Name, detachedHead.CanonicalName);

See more at "LibGit2Sharp.Tests/CheckoutFixture.cs#CanCheckoutAnArbitraryCommit()"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250