Search code examples
python-3.xgitgitpython

Clone repository and "checkout" to "tags/<my_tag>


I clone a repository using Repo.clone_from(). Afterwards, I simply want to switch to a specified tag within the repository, similar to git checkout tags/<my_tag>.

I found the following solution, but it resets HEAD of my masterbranch, which is not what I want:

        logging.info("Cloning repo... " + name)
        repo = Repo.clone_from(url, path)
        repo.head.reference = repo.tags[tag].commit

Is it possible to perform something like a git checkout tags/<my_tag> where I just switch to the current tag, but am able to switch also back to the current / latest commit on the repository?


Solution

  • I got the answer within the GitPython project QA:

    repo.git.checkout(tag)