DEV Community

Cover image for Adding Colab Notebooks to Github using Git
Rahul Bordoloi
Rahul Bordoloi

Posted on

Adding Colab Notebooks to Github using Git

Ever thought of pushing your Colab Notebook into your Github Repository Directly? Yes, It's possible and it's an anomaly of how we do it locally.

Firstly, Initialize an empty git repository as -

!git init
Enter fullscreen mode Exit fullscreen mode

Then, add your data to your Global Config-Files,

# Add your email to the global config-file
!git config --global user.email <your-email-add@email.com>

# Add your email to the global config-file
!git config --global user.name <your-github-username>
Enter fullscreen mode Exit fullscreen mode

Then add your files to the repo using -

!git add .
Enter fullscreen mode Exit fullscreen mode

NOTE: !git add. would add all the files and folders from your current directory. We can selectively select the files we want to add too by using -

!git add <file-name/directory-name>
Enter fullscreen mode Exit fullscreen mode

Then commit the added files with a message -

!git commit -m "Colab Commit"
Enter fullscreen mode Exit fullscreen mode

Now, Adding our remote repository into our colab system,

!git remote add origin https://<your-github-username>:<your-github-password>@github.com/<your-github-username>/<your-repository-name>.git
Enter fullscreen mode Exit fullscreen mode

Note: If we've activated 2FA Security on your account, then we must add our personal-access-token instead of our password which is available in Settings -> Developer Settings -> Personal Access Token ie replace <<your-github-password> with <personal-access-token>

And at last to push our committed files into our repository-

!git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Note: If any error occurs in this step regarding push, Try out

!git push -u origin master --force
Enter fullscreen mode Exit fullscreen mode

Troubleshooting -

If you get errors with origin like fatal: remote origin already exists, try out -

!git remote rm origin
Enter fullscreen mode Exit fullscreen mode

For any queries, mail me at - mail@rahulbordoloi.me

Also, check out my Gist for the same -

Thank You!

Top comments (1)

Collapse
 
hb profile image
Henry Boisdequin

Cool and out of the box! Thanks!