你如何用 pygit2 结帐分支?

neb*_*ffa 6 python git pygit2

我想用来pygit2结帐分支名称。

例如,如果我有两个分支:masterandnewHEADis at master,我希望能够做到:

import pygit2
repository = pygit2.Repository('.git')
repository.checkout('new')
Run Code Online (Sandbox Code Playgroud)

甚至

import pygit2
repository = pygit2.Repository('.git')
repository.lookup_branch('new').checkout()
Run Code Online (Sandbox Code Playgroud)

但两者都不起作用,pygit2 文档没有提到如何结帐分支。

neb*_*ffa 9

看来你可以这样做:

import pygit2
repo = pygit2.Repository('.git')
branch = repo.lookup_branch('new')
ref = repo.lookup_reference(branch.name)
repo.checkout(ref)
Run Code Online (Sandbox Code Playgroud)