How to checkout a tag with GitPython

Nicolas BOISSINOT picture Nicolas BOISSINOT · Nov 19, 2013 · Viewed 11.7k times · Source

In a python script, I try to checkout a tag after cloning a git repository. I use 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"])

With this code I have an error:

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.

If I replace the tag name with a branch name, I have no problem. I didn't find informations in GitPython documentation. And if I try to checkout the same tag in a shell, I have non problem.

Do you know how can I checkout a git tag in python ?

Answer

lev picture lev · Jan 8, 2015

Assuming you cloned the repository in 'path/to/repo', just try this:

from git import Git

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

g.checkout('tag_name')