Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/GitHub/GitHub_Clone_repository.ipynb
2973 views
Kernel: Python 3

GitHub.png

GitHub - Clone repository

Give Feedback | Bug report

Tags: #github #snippet #operations #repository

Last update: 2023-05-09 (Created: 2022-12-07)

Input

Import libraries

import os

Setup Variables

  • repo_url: URL of the repository to clone

  • output_dir: Output directory to clone repo. If None, we will create a folder with the name of the repo

# Inputs repo_url = "https://github.com/jupyter-naas/awesome-notebooks" # Outputs output_dir = None

Model

Clone repository

Clone the repository from the given URL and create a local copy of it.

def clone_branch(repo_url, output_dir): # Get GitHub owner and repo name owner = repo_url.split("https://github.com/")[-1].split("/")[0] repo_name = repo_url.split("/")[-1] # Add repo name with .git extension if not repo_name.endswith(".git"): repo_name = f"{repo_name}.git" repo = f"{owner}/{repo_name}" # Init output dir if not output_dir: output_dir = repo_name[:-4] # Create output directoy if not os.path.exists(output_dir): os.makedirs(output_dir) # GitHub Action !cd '{output_dir}' !git clone git@github.com:'{repo}' '{output_dir}' print(f"✅ GitHub repo cloned: {output_dir}") return output_dir

Output

Clone repository

output_dir = clone_branch(repo_url, output_dir)