1

I have a GitHub repository and I would like to make a copy of that repository at every commit throughout the history of that repository without altering it. I've been using the GitPython package in Python, and here is what I have so far:

from git import *
import os    

for file in os.listdir(folder)[1:len(os.listdir(folder))]:
directory = folder + file

# create repo object for each team
repo = Repo(directory)

# list of all commits in the repo
commits = list(repo.iter_commits('master'))
for c in commits:
    # unique SHA key
    sha = c.name_rev.split()[0] 
    repo.clone(sha)

However, this doesn't work -- it simply clones the repo (the final one) such that I get many copies of the same thing. Essentially repo.clone() is simply taking renaming each copy for the specified sha rather than using it as a unique identifier. There is another function repo.clone_from() that should take the URL as an argument, but I don't know how to retrieve the URL of a specific commit.

Maybe using -bash commands is a better way to do this?

emillyrock
  • 77
  • 7
  • You should be able to clone, then do a hard reset to the specific sha. – adiaz004 Aug 01 '17 at 19:09
  • @adiaz004 as in using bash commands? – emillyrock Aug 02 '17 at 00:24
  • check out this link https://stackoverflow.com/questions/11864735/how-to-do-a-git-reset-hard-using-gitpython – adiaz004 Aug 02 '17 at 02:26
  • @adiaz004 `repo.head.reset('HEAD~1', index=True, working_tree=True)` has worked up until I reach a commit that is a merge. Is there a way around this as all I want is to iterate through the commands on the master branch? I get this error: `stderr: 'fatal: Failed to resolve 'HEAD~1' as a valid revision.'` – emillyrock Aug 02 '17 at 18:37

0 Answers0