How to Install and Use Git in Google Colab?
Colab or Colaboratory is a cloud-based Jupyter environment from Google. It was developed by the Research Team at Google to build and test machine learning or deep learning algorithm that requires high computational power. It is available to anyone with a Google account free of cost with some limitations on GPUs and TPUs. Git is a free and open-source distributed version control system created by Linus Torvalds in 2005. Its features include support for distributed, non-linear workflows, branching, pull requests, merging, tracking history, and a very easy learning curve.
In this article, we will discuss how to use git in Google Colab to clone a private repository, commit some new changes and push these changes to the remote repository. The process is simple and straight word if you are familiar with git. Fortunately, Google Colab comes with git pre-installed. So, all we need is to run some shell commands by using the ! operator.
Installation of Git in Colab
Step 1: Verify the git installation on the machine by running
Step 2: Update git configuration settings by adding your email and username to the commit.
!git config –global user.email “email”
!git config –global user.name “username”
Step 3: Before cloning the repository, generate a personal access token by visiting https://github.com/settings/tokens Make sure to give the required privileges to the token.
Step 4: Clone the required repository using the PAT.
!git clone https://firstname.lastname@example.org/username/repository.git
Step 5: Change the working directory to the newly cloned folder by using the % operator.
Step 6: Now, we can create, update or delete any file in our cloned repository. But for now, I am creating just a dummy readme file.
!echo “# Some dummy text” >> new.md
Step 7: Commit the new file and push the changes to the main branch.
!git add .
!git commit -m “relevant message”
!git push origin branch_name
Step 8: Verify the commit in the git logs or visit the repo on Github/GitLab.