In a python script, I try to create and push a tag to origin on a git repository. I use gitpython-1.0.2.
I am able to checkout an existing tag but have not found how to push a new tag to remote.
Many thanks
new_tag = repo.create_tag(tag, message='Automatic tag "{0}"'.format(tag))
repo.remotes.origin.push(new_tag)
new_tag.name here.To create a new tag using gitpython:
from git import Repo
obj = Repo("directory_name")
obj.create_tag("tag_name")
To push to remote
obj.remote.origin.push("tagname")
Git supports two types of tags: lightweight and annotated. So in GitPython we also can created both types:
# create repository
repo = Repo(path)
# annotated
ref_an = repo.create_tag(tag_name, message=message)
# lightweight
ref_lw = repo.create_tag(tag_name)
To push a lightweight tag you need specify the reference to tag repo.remote('origin').push(ref_lw), but for an annotated tag you can just use:
repo.remote('origin').push()
if configuration push.followTags = true. To set configuration programmatically
repo.config_writer().set_value('push', 'followTags', 'true').release()
Additional information about pushing commits & tags simultaneously