0

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.

2 Answers 2

1

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("")
Sign up to request clarification or add additional context in comments.

Comments

0

putted together info from two resources to clone all organizations private repositories:

# https://www.thepythoncode.com/article/using-github-api-in-python
# https://github.com/libgit2/pygit2/issues/554

# pip3 install PyGithub pygit2

from github import Github
import pygit2

# using an access token
g = Github('TOKEN')
org = g.get_organization('ORG NAME')

callbacks = pygit2.RemoteCallbacks(pygit2.UserPass('TOKEN', 'x-oauth-basic'))
# Clone repo
for repo in org.get_repos():
    pygit2.clone_repository(
        url=repo.clone_url,
        path=f'/home/<Username>/PycharmProjects/Backup/{repo.name}',
        callbacks=callbacks)

1 Comment

This script does not work. Getting File "script.py", line 16, in <module> _pygit2.GitError: too many redirects or authentication replays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.