Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout a tag with GitPython

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 ?

like image 952
Nicolas BOISSINOT Avatar asked Nov 19 '13 14:11

Nicolas BOISSINOT


People also ask

Can we checkout a tag?

You will not be able to checkout the tags if it's not locally in your repository so first, you have to fetch the tags to your local repository. Instead of origin use the tags/ prefix. In this sample you have 2 tags version 1.0 & version 1.1 you can check them out with any of the following: $ git checkout A ...

How do I clone a git repository with a tag?

git clone $ git clone -b <tagname> <repository> . If you only need the specific tag, you can pass the --single-branch flag, which prevents fetching all the branches in the cloned repository. With the --single-branch flag, only the branch/tag specified by the --branch option is cloned.

How do I run a git clone command in Python?

We can use git module in python to clone the repository from git. Clone the repository you want to work with in local system. So in clone_from methods pass the two arguments in which first argument is url of your repository and second argument is the location of your directory where you want to cloned the repo.


1 Answers

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')
like image 52
lev Avatar answered Sep 21 '22 02:09

lev