13

在 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 标签吗?

4

3 回答 3

10

假设您在 'path/to/repo' 中克隆了存储库,请尝试以下操作:

from git import Git

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

g.checkout('tag_name')
于 2015-01-08T13:34:23.623 回答
1
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.
""" 
于 2016-12-29T09:51:33.570 回答
-1

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

from git import Repo

repo = Repo.clone_from("https://url_here", "local_path")
repo.heads['tag-name'].checkout()
于 2018-11-19T21:46:06.153 回答