在 python 脚本中,我尝试创建一个标签并将其推送到 git 存储库上的原点。我使用gitpython-1.0.2.
我能够签出现有标签,但尚未找到如何将新标签推送到远程。
非常感谢
new_tag = repo.create_tag(tag, message='Automatic tag "{0}"'.format(tag))
repo.remotes.origin.push(new_tag)
使用 gitpython 创建新标签:
from git import Repo
obj = Repo("directory_name")
obj.create_tag("tag_name")
推送到远程
obj.remote.origin.push("tagname")
tag = repo.create_tag(tagName, message=mesg)
repo.remote.origin.push(tag.path)
tag.name
可能与您本地的分支名称冲突,请在此处使用tag.path
。
Git 支持两种类型的标签:轻量级标签和带注释的标签。 所以在 GitPython 中我们也可以创建这两种类型:
# create repository
repo = Repo(path)
# annotated
ref_an = repo.create_tag(tag_name, message=message))
# lightweight
ref_lw = repo.create_tag(tag_name))
要推送轻量级标签,您需要指定对标签
repo.remote('origin').push(ref_lw )
的引用,但在带注释的情况下,您可以只使用:
repo.remote('origin').push()
如果配置
push.followTags = true
。以编程方式设置配置
repo.config_writer().set_value('push', 'followTags', 'true').release()
我使用下面的代码片段创建一个标签并将其推送到远程。您可能需要通过在每个 git 操作上添加 try ... catch 块来处理异常。
repo = git.Repo(os.path.abspath(repo_path)
repo.git.tag('-a', tagname, commit, '-m', '')
repo.git.push('origin', tagname)