Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用GitPython签出标记_Python_Git_Gitpython - Fatal编程技术网

如何使用GitPython签出标记

如何使用GitPython签出标记,python,git,gitpython,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 "/

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

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

from git import Git

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

g.checkout('tag_name')
就像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.
""" 

这对我来说很有效,我认为它更接近预期的API用途:

from git import Repo

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

我希望这只是一个示例,但您的错误表明您实际使用的是字符串
“tag_name”
,这就是错误发生的原因。无论如何,
git checkout
是正确的格式,但是您还应该知道,您应该首先
git fetch
,然后
git pull origin refs/tags/
after.AttributeError:'git'对象没有属性'checkout',我刚刚尝试了序列:[1]从git import git[2]g=git('GooglePlayAppsCrawler')[3]g.checkout()。它可以工作(GooglePlayAppsCrawler是我在当前工作目录中拥有的git repo)。对我不起作用:AttributeError:'IterableList'对象没有属性
标记名
这对我起作用了-我相信这符合gitpython的最新版本(gitpython==3.1.7)
from git import Repo

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