Methods for using Git with Google Colab

Are there any recommended methods to integrate git with colab?

For example, is it possible to work off code from google source repositories or the likes?

Neither google drive nor cloud storage can be used for git functionality.

So I was wondering if there is a way to still do it?


Solution 1:

If you want to clone a private repository, the quickest way would be to create a personal access token and select only privileges that your application needs. Then clone command for GitHub would look like:

!git clone https://[email protected]/username/repository.git

Solution 2:

git is installed on the machine, and you can use ! to invoke shell commands.

For example, to clone a git repository:

!git clone https://github.com/fastai/courses.git

Here's a complete example that clones a repository and loads an Excel file stored therein. https://colab.research.google.com/notebook#fileId=1v-yZk-W4YXOxLTLi7bekDw2ZWZXWW216

Solution 3:

The very simple and easy way to clone your private github repo in Google colab is as below.

  1. Your password won't be exposed
  2. Though your password contains special character also it works
  3. Just run the below snippet in Colab cell and it will execute in an interactive way
import os
from getpass import getpass
import urllib

user = input('User name: ')
password = getpass('Password: ')
password = urllib.parse.quote(password) # your password is converted into url format
repo_name = input('Repo name: ')

cmd_string = 'git clone https://{0}:{1}@github.com/{0}/{2}.git'.format(user, password, repo_name)

os.system(cmd_string)
cmd_string, password = "", "" # removing the password from the variable

Solution 4:

You can use ssh protocol to connect your private repository with colab

  1. Generate ssh key pairs on your local machine, don't forget to keep
    the paraphrase empty, check this tutorial.

  2. Upload it to colab, check the following screenshot

    from google.colab import files
    uploaded = files.upload()

  3. Move the ssh kay pairs to /root and connect to git

    • remove previously ssh files
      ! rm -rf /root/.ssh/*
      ! mkdir /root/.ssh
    • uncompress your ssh files
      ! tar -xvzf ssh.tar.gz
    • copy it to root
      ! cp ssh/* /root/.ssh && rm -rf ssh && rm -rf ssh.tar.gz ! chmod 700 /root/.ssh
    • add your git server e.g gitlab as a ssh known host
      ! ssh-keyscan gitlab.com >> /root/.ssh/known_hosts
      ! chmod 644 /root/.ssh/known_hosts
    • set your git account
      ! git config --global user.email "email"
      ! git config --global user.name "username"
    • finally connect to your git server
      ! ssh [email protected]
  4. Authenticate your private repository, please check this Per-repository deploy keys.

  5. Use ! [email protected]:{account}/{projectName}.git
    note: to use push, you have to give write access for
    the public ssh key that you authenticate git server with.