Using git & GitHub on Google Colaboratory

Farhan Hai Khan
Geek Culture
Published in
4 min readJun 1, 2021

Ever tried using git/GitHub on Google Colaboratory? Want to shift some insanely large directory from Google Drive to your GitHub Repository? Want to directly push Machine Learning models trained on Colab GPUs to GitHub?

Fret not, we will demystify this very topic today Using git & GitHub on Google Colaboratory! You can follow along by creating a copy of this Google Colaboratory Notebook into your own Drive.

The processes/methodologies described in this notebook works on colab for both public and private repositories. Try not to modify/skip any steps unless you are a git/GitHub expert! Replace all {variables} with the actual names that they contain.

Getting Started Prerequisites — User Information Needed :

There are some {variables} that you will need before actually proceeding to change/modify during the process and replace with your own values.

  • {your_username} - Your GitHub UserName.
  • {your_email_id} - The Email Address associated with your GitHub Account.
  • {your_password} - The Password that you use to login to your GitHub Account. Update September 2021 — For security reasons, passwords are now deprecated for github usage. Please use the `Personal Access Token` instead — Go to github.com -> Settings ->Developer Settings -> Personal Access Token and generate a token for the required purpose. Use this in place of your password for all tasks mentioned along this tutorial!
  • {destination_repo_projectname} - The Remote GitHub Repository that you wish to work with (This could be other remote hosting Repos too, like GitLab/BitBucket).
  • {destination_repo_username} - The Owner of the Remote Repository that you wish to work on (could be yourself if you want to work on your own repository).

Cloning a Repository :

Cloning is the process of downloading a local copy of a remote repository with priviledges of version control pre-initiated into it! You can clone a remote git repository using the git clone command :

!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git

Example :

!git clone https://khanfarhan10:thatsmypassword@github.com/khanfarhan10/LungSegmentationCustomUNET.git

Note : Please note that we are using our git password for cloning. While not necessary for public repositories, it will cause a problem while remote pushing to the repository, so it’s best advised to clone with your password and overall credentials.

Changing the Current Working Directory

Change the directory to {destination_repo_username} using line magic command %cd for jupyter notebooks. The default Working Directory on Google Colaboratory is /content/. Since we cloned the repository without changing the original working directory, it should be present inside the /content/ directory. Perform thsi command to change the directory to inside your Local copy of the Remote Repository.

%cd /content/{destination_repo_username}

Example :

%cd /content/LungSegmentationCustomUNET

Verify your Steps:

It is probably well and good if you verify that you are going right. We probably don’t want to mess up with our GitHub Repository. However with practice you can skip these steps.

Pull

Perform Sanity Checks to see if everything works perfectly!

!git pull

If no changes were made to the remote git repo after cloning, the following should be the displayed output :

Already up to date.

Status

Similarly check the status of the staged/unstaged changes.

!git status

It should display this, with the default branch selected :

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Check Older Logs

Check the previous commits you have made on the repo :

!git log -n 4

Outputs Git Commit IDs with Logs :

commit 18ccf27c8b2d92b560e6eeab2629ba0c6ea422a5 (HEAD -> main, origin/main, origin/HEAD)
Author: Farhan Hai Khan <njrfarhandasilva10@gmail.com>
Date: Mon May 31 00:12:14 2021 +0530

Create README.md

commit bd6ee6d4347eca0e3676e88824c8e1118cfbff6b
Author: khanfarhan10 <njrfarhandasilva10@gmail.com>
Date: Sun May 30 18:40:16 2021 +0000

Add Zip COVID

commit 8a3a12863a866c9d388cbc041a26d49aedfa4245
Author: khanfarhan10 <njrfarhandasilva10@gmail.com>
Date: Sun May 30 18:03:46 2021 +0000

Add COVID Data

commit 6a16dc7584ba0d800eede70a217d534a24614cad
Author: khanfarhan10 <njrfarhandasilva10@gmail.com>
Date: Sun May 30 16:04:20 2021 +0000

Removed sample_data using colab (testing)

Make changes in the local repo

Make changes from the local repo directory.

These might include various additions, deletions, or edits.

Pro Tip : If you want you can copy paste things from drive to a git repo by:

Mount Google Drive:

from google.colab import drive
drive.mount('/content/gdrive')

Copy contents using Shell Utilities/ Google Drive :

import shutil,os

# For a folder:
shutil.copytree(src_folder,des_folder)

# For a file:
shutil.copy(src_file,des_file)

# Create a ZipFile
shutil.make_archive(archive_name, 'zip', directory_to_zip)

# Remove Folder
shutil.rmtree(folder_name)

# Remove File
os.remove(file_name)

Set Git Credentials

Tell git who exactly are you?

!git config --global user.name "{your_username}"
!git config --global user.email "{your_email_id}"
!git config --global user.password "{your_password}"

Example :

!git config --global user.name "khanfarhan10"
!git config --global user.email "njrfarhandasilva10@gmail.com"
!git config --global user.password "thatsmypassword"

Check Remote Repository URL

This is the final step to ensuire that no errors have crept in. To check if the remote url is set and configured correctly, Run :

!git remote -v

If configured properly it should output the following :

origin    https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git (fetch)
origin https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git (push)

Example :

origin    https://khanfarhan10:thatsmypassword@github.com/khanfarhan10/LungSegmentationCustomUNET.git (fetch)
origin https://khanfarhan10:thatsmypassword@github.com/khanfarhan10/LungSegmentationCustomUNET.git (push)

Add, Commit, Push

Just the Regular Stuff : add (all) the files that you wish to change to your staging area. commit or create a version of the repository that you wish to update. push or upload your recent changes to your remote git repository. You know what to do.

!git add .
!git commit -m "{Message}"
!git push

Enjoy the power of git and GitHub with Google Colaboratory! If you wish to ask a question, feel free to contact me on LinkedIn.

TL;DR Complete Process in Short :

!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git
%cd /content/{destination_repo_projectname}

!git config --global user.name "{your_username}"
!git config --global user.email "{your_email_id}"
!git config --global user.password "{your_password}"

Make Your Changes and then run :

!git add .
!git commit -m "{Message}"
!git push

--

--

Farhan Hai Khan
Geek Culture

AI Team @DSC-IEM | WoC ’20 Mentor | ML Intern @IIT KGP | Independent Researcher | Electrical Engineering | IEM Kolkata