Search code examples
pythongithubautomationclonegit-clone

python script to clone all the code repos from github code organisation


There are many ways to clone the GitHub code repos from an organization. Now considering there are some repos which are private and public, I was looking into a solution where an automated script or code logic would solve my purpose of cloning the code repos irrespective of repo type. I tried various measures but couldn't land to simple automated solution. I tried using curl command, but it was slow as the density of code repos to be cloned was above 200+ code repos.


Solution

  • Can you try below python code snippet to clone all repos:

    Here we use git ssh to clone each repos in a secure way to accommodate both public/private code.

    Sometimes due to network issues , it may show remote hung/packet-loss messages which leads to repo partial/no cloning. to avoid that please set below parameter in the shell.

    git config --global http.postBuffer 157286400
    

    Assuming that your git global credentials are updated.

    Code :

    import os
    # Define your repo list
    list = ["repo1","repo2"]
    
    #Loop it through repo's list
    for repo in list:
        #Clone the each repo using ssh git clone command in most secure way
        cmd = "git clone git@<git ssh url>/{}".format(repo)
        print("Starting to clone {}".format(repo))
        os.system(cmd)
        print("Finshed cloning {}".format(repo))
        print("#####################################")
        print("")