How to push into Github from Google Colab?

Hello fellows 👋. Hope you’re all having some productive time during this pandemic.

Here we’ll be seeing how to push files into your github repo while working in google colab after you’ve cloned a repo in google drive.

Google colab is one of the best feature of google I must say. And it has been a good tool for those having poor laptop configuration in order to open Notebook through Python Anaconda though there are other alternatives as well.

First thing you’ll need to do is generate a github token, one like an SSH key. Pushing into github is only possible through github API for which unique access token will be required. But this can also be done without needing to generate an unique token and just by passing password and user name along with the github link. To generate a unique token:
1. Go to your github settings.
2. On the left at the button there is an option, ‘Developer Settings’
3. Then again on the left, click on ‘Personal access tokens’ option
4. Then click on ‘Generate new token’, enter you github password, give a name, tick all the option and now you have your new token.

After we have our access token, let us now go to our notebook in colab. If you’ve not mounted your google drive then follow the steps below to mount your google drive.
1. from google.colab import drive
2. drive.mount(‘/content/gdrive/)
After this you’ll need to authorize permission to access the content from drive. Just click the link below which will open another tab for you. Select your gmail account, allow access to drive. After that you’ll see an authorization code. Just copy it and paste it in the box and hit enter.
3. Now change working directory to your working directory
%cd gdrive/MyDrive/your-working-directory/

After we’ve successfully mounted, it’s compulsory for us to identify ourselves before making commit to the repo. So let’s configure our email and name.
!git config — global user.emailyour-email
!git config — global user.nameyour-github-name
Also let’s assure that all the changes in the remote repository is up-to-date in our cloned repository also.
!git pull origin master

Now it’s like usual add, commit and push command.
1. !git add .
2. !git commit -myour comment
3. !git push https://your-github-access-token@github.com/your-github-name/your-github-repo-name.git
Please note here about your github access token. Go to the tab where he had generated new github access token. Just copy and paste above. Also be careful about your github name and github repo name. Once it’s done, just hit enter. Now go to your github repo and see the magic now.

Bamm!! This is one of the ways to directly push into github repo from your google colab to your github repo. The method which I just talked about is quite clumsy as you need to generate github token every time Google Colab session ends. But you could also search for an easier method, just as I had mentioned above, where you are supposed to pass your github-password and github-user-name in the url instead of generating github token.

Thanks for taking your time and reading my article. Hope this was helpful. If you found it interesting, give a clap.