Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone repository and “checkout” to "tags/<my_tag>" #1026

Closed
conFusl opened this issue Jul 6, 2020 · 2 comments
Closed

Clone repository and “checkout” to "tags/<my_tag>" #1026

conFusl opened this issue Jul 6, 2020 · 2 comments
Labels
Q&A

Comments

@conFusl
Copy link

@conFusl conFusl commented Jul 6, 2020

More a general Q/A than an issue, but I'm struggling with this topic.

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 master branch, 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?

@Byron Byron added the Q&A label Jul 6, 2020
@Byron
Copy link
Member

@Byron Byron commented Jul 6, 2020

That's quite alright, the issue was tagged accordingly. Thanks.

That's a great finding! I did do some searching to come up with the following.

  • detach the HEAD to point to the tag: repo.head.set_reference(repo.tags[tag].commit
  • reset the working tree to the current commit, but… it seems wonky to do it like this especially since there is repo.git.checkout(tag) which does exactly what you want.

I hope this answers the question - GitPython is a lot of 'low-level' plumbing, which makes some common operations unintuitive. These are best left to the git porcelain, provided by the git command-line interface.

Even though I am closing the issue, please feel free to keep posting follow-ups.

@Byron Byron closed this Jul 6, 2020
@conFusl
Copy link
Author

@conFusl conFusl commented Aug 4, 2020

Thanks a lot for the reply.
I was struggling to follow the API description - and where I have which possibilities, e.g. repo, git, head, tags[]...

Your solution does exactly what I expected, and I was pretty sure that there is a possibility :-) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.