将存储库和 "checkout" 克隆到 "tags/<my_tag>

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

我使用 Repo.clone_from() 克隆了一个存储库。之后,我只想切换到存储库中的指定标签,类似于 git checkout tags/<my_tag>.

我找到了以下解决方案,但它重置了我的 masterbranch 的 HEAD,这不是我想要的:

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

是否可以执行类似 git checkout tags/<my_tag> 的操作,我只切换到当前标签,但也可以切换回存储库中的当前/最新提交?

我在 GitPython project QA 中得到了答案:

repo.git.checkout(tag)