How to Upload Project on GitHub from Google Colab?


In the field of programming and software development, GitHub has emerged as an essential platform for hosting and exchanging code and projects. It provides a collaborative space for developers to work together, handle version control, and exhibit their projects globally. Concurrently, Google Colab has garnered favor among data scientists and machine learning aficionados as a robust online coding environment.

By integrating the strengths of both platforms, a smooth workflow for project development and sharing can be achieved. This article aims to lead us through the process of uploading a project from Google Colab to GitHub, offering novice-friendly, step-by-step guidelines and valuable insights.

Setting up the Environment

Before delving into the procedure, let's make sure that everything is set up appropriately. Initially, verify that we possess a Google account and the ability to utilize Google Colab. If we do not have an account, it's simple to create one without any cost. Following that, confirm that we also possess a GitHub account. If we don't possess one, navigate to the GitHub website and register. Once both accounts are created, we can proceed with the next steps.

Creating a New Repository on GitHub

Let's begin by creating a fresh repository on GitHub. Sign in to the GitHub account and locate the "+" icon positioned in the upper-right corner. From the options presented, choose "New repository." Provide a distinctive name for the repository, include a description if desired, and specify whether it should be accessible to the public or remain private. Once we have entered the necessary details, proceed by selecting the "Create repository" button to finalize the process.

Configuring Google Colab

Now that our repository is created on GitHub, let's customize Google Colab to collaborate with it. Access Google Colab and either generate a new notebook or access a pre-existing one. Initially, we must install Git and arrange it within the notebook environment. Execute the below code within a code cell −

!apt-get install -qq git

Next, we need to authenticate Google Colab with our GitHub account. Run the following code snippet, replacing <YOUR_GITHUB_USERNAME> with our actual GitHub username −

Now, the next step involves verifying the authenticity of Google Colab using our GitHub account. Execute the provided code snippet, substituting <YOUR_GITHUB_USERNAME> with the real GitHub username.

from getpass import getpass
import os
os.environ['GITHUB_USERNAME'] = getpass('GitHub username:')
os.environ['GITHUB_TOKEN'] = getpass('GitHub token:')

Enter the GitHub username and access code when prompted by the above. To create a code, visit the GitHub profile settings, go to the "Developer options" section, and select "Personal access keys." Generate a fresh key with the required authorizations and make a copy.

Cloning the Repository

Now that we have finished setting up the configuration, we can proceed with cloning the GitHub repository into our Google Colab environment. In a code cell, execute the given instruction, making sure to replace "<YOUR_GITHUB_USERNAME>" with our own GitHub username and "<REPO_NAME>" with the name of our repository −

!git clone https://github.com/<YOUR_GITHUB_USERNAME>/<REPO_NAME>.git

This command above will clone the repository to the current directory in Google Colab.

Uploading Files and Making Changes

Now that we have successfully cloned our repository in Google Colab, we can easily integrate supplementary or any additional files and make any required modifications or edits. To add files to the repository, just use the file browser situated on the left-hand panel of the Colab interface and seamlessly drag and release the desired files into the folder of the duplicated repository.

To make alterations to the project, open the specific file within Colab, execute the desired modifications, and securely save the changes. Furthermore, the option to generate new files directly within Colab is also available. Once all the required adjustments have been made, it is essential to commit and push these updates to the GitHub repository to ensure proper synchronization and version control.

Committing and Pushing Changes

To apply the modifications or commit any changes, go to the directory of the repository using the file explorer located on the left side of Google Colab. Next, click with the right mouse button on the folder and choose the option " Open in the terminal." Once the terminal opens, execute the given instructions below −

git add .
git commit -m "Your commit message"
git push

In the above code, the first step incorporates all the modifications into the Git repository. Following that, the second step generates a commit containing a detailed explanation of the alterations performed. Lastly, the third step pushes the commit to GitHub, syncing the local repository with the remote repository.

Verifying the Upload

We have effectively uploaded the project to GitHub from Google Colab. To confirm the transfer, kindly navigate to the GitHub repository page and ensure that the files and modifications are accurately displayed. Moreover, we can engage in collaborative efforts with fellow developers, share the project with a wider audience, or even deploy it utilizing the supplementary functionalities offered by GitHub.

Conclusion

In this article, we have explored the process of uploading a project to GitHub from Google Colab. By merging the advantages of these two platforms, we can optimize our work process and effortlessly distribute our projects to others. We have discussed a detailed process, beginning from the initial setup of the environment to the process of committing and pushing modifications.

Updated on: 08-Aug-2023

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements