简体   繁体   English

如何使用GitPython标记特定提交

[英]How do I tag a specific commit with GitPython

I have created a simple script which uses GitPython to tag the latest commit on a specific branch which has already been checked out. 我创建了一个简单的脚本,它使用GitPython来标记已经检出的特定分支上的最新提交。

from git import Repo

def TagRepo(path, tag):
    repo = Repo(path)
    repo.create_tag(tag)
    repo.remotes.origin.push(tag)


if __name__ == "__main__":

    parser = optparse.OptionParser('usage: %prog [options] ')
    parser.add_option('-p', '--path', dest='path', help='path to repo')
    parser.add_option('-t', '--tag', dest='tag', help='Tag label')

    (options, args) = parser.parse_args()

    TagRepo(options.path, options.tag)

I want to improve it so that I can pass it a specific commit's SHA and tag that instead of the latest commit on the branch but I can't see how to do that with GitPython. 我想改进它,以便我可以传递一个特定的提交的SHA和标记,而不是分支上的最新提交,但我不知道如何使用GitPython。

From the documentation of the module : 模块的文档:

Obtain commits at the specified revision 获取指定修订版的提交

    repo.commit('master')
    repo.commit('v0.8.1')
    repo.commit('HEAD~10')

So to retrieve a specific commit just use repo.commit('SHA-1') 因此,要检索特定的提交,请使用repo.commit('SHA-1')

From the source code: 从源代码:

def create_tag(self, path, ref='HEAD', message=None, force=False, **kwargs)

it appears you can pass a ref to create_tag which following the comments down the line is described as: 看来你可以将ref传递给create_tag,其后面的注释被描述为:

:param ref: A reference to the object you want to tag. :param ref:对要标记的对象的引用。 It can be a commit, tree or blob. 它可以是提交,树或blob。

so just get the commit you want and pass that as the ref. 所以只需获得你想要的提交并将其传递给ref。 dont foget to push as OP does. 不要像OP那样努力推动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM