The convenient way to cherry-pick a commit is to use Repository.cherrypick(). It is limited to cherry-picking with a working copy and on-disk index.

$ cd /path/to/repo
$ git checkout basket
$ git cherry-pick 9e044d03c
repo = pygit2.Repository('/path/to/repo')

cherry_id = pygit2.Oid('9e044d03c')

if repo.index.conflicts is None:
    tree_id = repo.index.write_tree()

    cherry    = repo.get(cherry_id)
    committer = pygit2.Signature('Archimedes', '')

    repo.create_commit(,, committer,
                       cherry.message, tree_id, [])
    del basket # outdated, prevent from accidentally using it


Cherry-picking a commit without a working copy

This way of cherry-picking gives you more control over the process and works on bare repositories as well as repositories with a working copy. merge_trees() can also be used for other tasks, for example three-argument rebases.

repo = pygit2.Repository('/path/to/repo')

cherry = repo.revparse_single('9e044d03c')
basket = repo.branches.get('basket')

base_tree = cherry.parents[0].tree

index = repo.merge_trees(base_tree, basket, cherry)
tree_id = index.write_tree(repo)

author    =
committer = pygit2.Signature('Archimedes', '')

repo.create_commit(, author, committer, cherry.message,
                   tree_id, [])
del None # outdated, prevent from accidentally using it