How to Install and Use Git in Google Colab?
Google Colab is a popular platform for data scientists and machine learning enthusiasts to write and execute Python code in a Jupyter Notebook environment. While it provides a robust environment for coding, integrating version control systems like Git can enhance productivity by allowing you to track changes, collaborate with others, and maintain project history. This article will guide you through the process of installing and using Git in Google Colab.
Why Use Git in Google Colab?
- Version Control: Keep track of changes and revert to previous versions if needed.
- Collaboration: Work seamlessly with other developers by sharing and merging code.
- Backup: Ensure your code is safely stored and accessible from any machine.
Installation of Git in Colab
Step 1: Verify the git installation on the machine by running
git version

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://git_token@github.com/username/repository.git


Step 5: Change the working directory to the newly cloned folder by using the % operator.
%cd folder_name

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.
!git log


