How do i push to remote with pygit2?

3k Views Asked by At

i want to clone a repository, change a file and push these changed file back to the origin branch. I can clone the repo with

repo = pygit2.clone_repository(repo_url, local_dir, checkout_branch="test_it")

but what do i need to do now to push the changes to the remote? I want only commit the changes for one specific file, even if more files are changed.

Hope someone can help me. TIA

1

There are 1 best solutions below

0
On

First stage only file_path:

# stage 'file_path'
index = repository.index
index.add(file_path)
index.write()

Then do a commit:

# commit data
reference='refs/HEAD'
message = '...some commit message...'
tree = index.write_tree()
author = pygit2.Signature(user_name, user_mail)
commiter = pygit2.Signature(user_name, user_mail)
oid = repository.create_commit(reference, author, commiter, message, tree, [repository.head.get_object().hex])

and last push the repo as described in Unable to ssh push in pygit2