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!
For more details you can also see my article on Medium : https://medium.com/geekculture/using-git-github-on-google-colaboratory-7ef3b76fe61b
None of the answers provide a straight and direct answer like this one :
GitColab
Probably this is the answer you are looking for..
Works on colab for both public and private repositories and don't change/skip any step: (Replace all {vars}
)
TL;DR Complete Process:
!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git
%cd /content/{destination_repo_username}
!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
Cloning a Repository :
!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git
Change the directory to
Change the directory to {destination_repo_username} using line magic command %cd
for jupyter notebooks.
%cd /content/{destination_repo_username}
Verify!
Pull
Sanity Check 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 <[email protected]>
Date: Mon May 31 00:12:14 2021 +0530
Create README.md
commit bd6ee6d4347eca0e3676e88824c8e1118cfbff6b
Author: khanfarhan10 <[email protected]>
Date: Sun May 30 18:40:16 2021 +0000
Add Zip COVID
commit 8a3a12863a866c9d388cbc041a26d49aedfa4245
Author: khanfarhan10 <[email protected]>
Date: Sun May 30 18:03:46 2021 +0000
Add COVID Data
commit 6a16dc7584ba0d800eede70a217d534a24614cad
Author: khanfarhan10 <[email protected]>
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, edditions, deletions, 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 shutil :
import shutil
# 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)
Set Git Credentials
Tell Git Who You Are?
!git config --global user.name "{your_username}"
!git config --global user.email "{your_email_id}"
!git config --global user.password "{your_password}"
Check Remote Again
Check if the remote url is set and configured correctly :
!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)
Add, Commit, Push
You know what to do.
!git add .
!git commit -m "{Message}"
!git push
Enjoy!