如何使用pygit2执行rebase?(How can I perform a rebase with pygit2?)

这个问题涉及如何与pygit2进行合并,但是,据我所知,这将导致新的提交。 有没有办法执行一个rebase,它不会导致新的提交,只会快速转发分支引用以对应给定远程的最新提交?

This question touches on how to perform a merge with pygit2, but, to the best of my understanding, that will result in a new commit. Is there a way to perform a rebase, which will not result in a new commit and will simply fast-forward the branch reference to correspond to the latest from a given remote?

最满意答案

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

示例(快速转发master到origin/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)

You can fast-forward with Reference.set_target().

Example (fast-forwarding master to origin/master, assuming that the script starts from checked out master branch in clean state):

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)如何使用pygit2执行rebase?(How can I perform a rebase with pygit2?)

这个问题涉及如何与pygit2进行合并,但是,据我所知,这将导致新的提交。 有没有办法执行一个rebase,它不会导致新的提交,只会快速转发分支引用以对应给定远程的最新提交?

This question touches on how to perform a merge with pygit2, but, to the best of my understanding, that will result in a new commit. Is there a way to perform a rebase, which will not result in a new commit and will simply fast-forward the branch reference to correspond to the latest from a given remote?

最满意答案

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

示例(快速转发master到origin/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)

You can fast-forward with Reference.set_target().

Example (fast-forwarding master to origin/master, assuming that the script starts from checked out master branch in clean state):

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)