如何使用GitPython签出标签

时间:2013-11-19 14:15:17

标签: python git gitpython

在python脚本中,我尝试在克隆git存储库后签出标记。 我使用GitPython 0.3.2。

#!/usr/bin/env python
import git
g = git.Git()
g.clone("user@host:repos")
g = git.Git(repos)
g.execute(["git", "checkout", "tag_name"])

使用此代码我有一个错误:

g.execute(["git", "checkout", "tag_name"])
File "/usr/lib/python2.6/site-packages/git/cmd.py", line 377, in execute
raise GitCommandError(command, status, stderr_value)
GitCommandError: 'git checkout tag_name' returned exit status 1: error: pathspec 'tag_name' did not match any file(s) known to git.

如果我用分支名称替换标签名称,我没有问题。 我没有在GitPython文档中找到信息。 如果我尝试在shell中签出相同的标签,我没有问题。

你知道如何在python中签出一个git标签吗?

3 个答案:

答案 0 :(得分:6)

假设你在'path / to / repo'中克隆了存储库,试试这个:

from git import Git

g = Git('path/to/repo')

g.checkout('tag_name')

答案 1 :(得分:2)

from git import Git
g = Git(repo_path)
g.init()
g.checkout(version_tag)

像cmd.py一样,Git评论说

"""
The Git class manages communication with the Git binary.

It provides a convenient interface to calling the Git binary, such as in::

 g = Git( git_dir )
 g.init()                   # calls 'git init' program
 rval = g.ls_files()        # calls 'git ls-files' program

``Debugging``
    Set the GIT_PYTHON_TRACE environment variable print each invocation
    of the command to stdout.
    Set its value to 'full' to see details about the returned values.
""" 

答案 2 :(得分:-1)

这对我有用,我认为它更接近于预期的API用法:

from git import Repo

repo = Repo.clone_from("https://url_here", "local_path")
repo.heads['tag-name'].checkout()