GitPython create and push tags
Asked Answered
U

5

12

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

Ursal answered 7/3, 2016 at 14:2 Comment(0)
U
19
new_tag = repo.create_tag(tag, message='Automatic tag "{0}"'.format(tag)) 

repo.remotes.origin.push(new_tag)
Ursal answered 7/3, 2016 at 14:55 Comment(3)
useful links: git.Repo.create_tag and tutorialAntimonic
this doesn't push the tag for me. I see it locally, but not remotelyCalvinism
It's better to use new_tag.name here.Ineludible
T
8

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")
Thurible answered 3/11, 2017 at 12:42 Comment(1)
this doesn't push the tag for me. I see it locally, but not remotelyCalvinism
N
2
tag = repo.create_tag(tagName, message=mesg)
repo.remote.origin.push(tag.path)

tag.name may be conflict with your local branch name, use tag.path here.

Nataline answered 17/11, 2021 at 2:59 Comment(0)
S
1

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 lightweight tag u need specify reference to tag repo.remote('origin').push(ref_lw ), but in annotated u 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

Stelu answered 31/5, 2022 at 12:1 Comment(0)
C
0

I used the below snippet code to create a tag and push it to remote. You may need to handle exceptions by adding try ... catch block on each git operation.

repo = git.Repo(os.path.abspath(repo_path)
repo.git.tag('-a', tagname, commit, '-m', '')
repo.git.push('origin', tagname)
Caryophyllaceous answered 4/8, 2021 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.