How to clone private GitHub repository from Google Colab

Although Google Colab allows us to connect to a GitHub account, this integration is limited to opening notebook files. It does not support cloning repositories directly through the linked account.

In this post, we will create a GitHub Personal Access Token and store it in Google Colab Secrets. We will then retrieve the token from the Secret Manager and use it to clone a repository securely.

Step 1: In GitHub setting, creating a Personal Access Token

Managing your personal access tokens – GitHub Docs

Step 2: In Google Colab, creating Secrets

Step 3: Clone the source code

from google.colab import userdata
CODE_DIR = "./src"
gh_account = "youraccount"
gh_repo = "repositoryname"
gh_branch = "main"
gb_clone_url = "https://{userdata.get('GITHUB_PAS')}@github.com/{gh_account}/{gh_repo}.git"
get_ipython().system(f'git clone --depth 1 --branch "{gh_branch}" "{gb_clone_url}" "{CODE_DIR}"')

Step 4: Done

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I’m Hung

Welcome to my personal space. This is a small corner where I share ideas and things I find interesting,..

Let’s connect

Don’t hesitate to reach out if there’s anything we can discuss, or simply to connect :)