Replicating “git checkout <commit> with PyGit2

I am trying to replicate the behaviour of the command “git checkout (commit)” where (commit) is the reference to as specific commit and not a branch name.

When using this command, the ‘HEAD’ of the repository point to the commit (detached head) and the working directory is in the same state that it was in this commit.

For the moment, I managed to make the HEAD of the repository point to a commit with PyGit2 :

def go(self, repo_name, version):
    repo = pygit2.Repository(bdd[repo_name])
    #commit = repo.revparse_single(version)
    #repo.reset(version, pygit2.GIT_RESET_HARD)
    print repo.head_is_detached

My problem is that I can’t find how to rollback the working directory like the Git CLI does.
I tried using:

  • repo.checkout_head() : It doesn’t do anything on the working directory.
  • repo.checkout() : Crash with a GitError: X conflicts prevent checkout

Is there a way to replicate this behaviour without using Repository.reset(ref, pygit2.GIT_RESET_HARD) ?

  • Non value parameters in clone_from in gitpython
  • Change git hook credentials in Windows
  • Using git to manage virtualenv state: will this cause problems?
  • python syncdb does not automatically load initial_data.json fixture in Python Django
  • Installing Python package pathos from git
  • PyCharm: multiple projects in same window, independent version control
  • pip: pulling updates from remote git repository
  • Having trouble setting up dev server for Django and website
  • One Solution collect form web for “Replicating “git checkout <commit> with PyGit2”

    Low-level checkout is git read-tree -um HEAD $target && git update-ref HEAD $target; pygit2 apparently understands only the one-tree read and none of the options on that, so however it’s doing checkout and merge and any number of other operations it doesn’t offer more than a crude mock-up of actual git. It looks like you could kludge it with add a ref to your commit, check that out, then reset HEAD and delete the ref.

    Git Baby is a git and github fan, let's start git clone.