简体   繁体   中英

How do you checkout a branch with pygit2?

I want to use pygit2 to checkout a branch-name.

For example, if I have two branches: master and new and HEAD is at master , I would expect to be able to do:

import pygit2
repository = pygit2.Repository('.git')
repository.checkout('new')

or even

import pygit2
repository = pygit2.Repository('.git')
repository.lookup_branch('new').checkout()

but neither works and the pygit2 docs don't mention how to checkout a branch.

It seems you can do:

import pygit2
repo = pygit2.Repository('.git')
branch = repo.lookup_branch('new')
ref = repo.lookup_reference(branch.name)
repo.checkout(ref)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM