如何使用 pygit2 执行变基?

How can I perform a rebase with pygit2?

This question 涉及如何与 pygit2 执行合并,但据我所知,这将导致新的提交。有没有一种方法可以执行变基,这不会导致新的提交,只会简单地快进分支引用以对应于给定远程的最新版本?

您可以使用Reference.set_target()快进。

示例(快进 masterorigin/master,假设脚本从签出 master 干净状态的分支开始:

repo.remotes['origin'].fetch()
origin_master = repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE)
master = repo.lookup_branch('master')
master.set_target(origin_master.target)

# Fast-forwarding with set_target() leaves the index and the working tree
# in their old state. That's why we need to checkout() and reset()
repo.checkout('refs/heads/master')
repo.reset(master.target, pygit2.GIT_RESET_HARD)