1

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?

Community
  • 1
  • 1
Piotrek
  • 102
  • 7
  • that's not, strictly speaking, a rebase. That's a fast-forward merge. – Wayne Werner Jul 26 '16 at 21:27
  • @WayneWerner yes, you're right. I'll need to play around with `pygit2` to apply the changes on current branch *on top of* the latest state from the same branch on a remote. – Piotrek Jul 27 '16 at 15:51

1 Answers1

2

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)
Leon
  • 31,443
  • 4
  • 72
  • 97
  • will [repo.lookup_branch('origin/branch1',GIT_BRANCH_REMOTE)](http://www.pygit2.org/references.html#pygit2.Repository.lookup_branch) actually fetch the latest state from remote ('origin' in this case)? *edit*: markdown – Piotrek Jul 25 '16 at 21:28
  • @Piotrek No, `lookup_branch()` doesn't fetch. You must `fetch()` explicitly. See updated answer. – Leon Jul 26 '16 at 21:25