如何使用GitPython签出标记

2024-05-20 15:46:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: name标记git脚本名称executebinusr
3条回答
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.
""" 

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

from git import Git

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

g.checkout('tag_name')

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

from git import Repo

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

相关问题 更多 >