Step-by-Step Guide to Checking Out a Commit ID with GitPython

Featured Image

Git is an important tool for developers and testers alike. It is a great source for deploying applications and allows us to track changes and make documentation along with the development of the applications. Here we will learn how to use the GitPython library to check out certain Git commit IDs from the Python codebase.

GitPython is a Python library for interfacing with Git repositories. It allows you to clone repositories, checkout specific commit IDs, and track commit history. Install it using ‘pip install gitpython’, and use functions like ‘checkout()’ and ‘log()’ for Git operations.

Also Read: How To Use GitPython To Pull Remote Repository?

What are Git Commit IDs?

Before going in deep, we will first learn what are Git Commit IDs. In Git, commits are a confirmation or refer to the changes made to the application. Each commit is made by a unique hexadecimal value called commit ID or hash. These commit IDs help us keep track of changes and scroll through the commit history.

Setting up GitPython

Before proceeding with the documentation, you must install GitPython on your desktop. To set up the environment, use the following code in the terminal or command prompt.

pip install gitpython

You will see a message “Successfully installed” confirming you are ready to work with Git in Python.

Also Read: Using GitPython to List All Files Affected by a Commit

Checkout Git Commit ID

Now open your Python script and follow the steps given below:

Importing Library

First, import the library into your Python file using the ‘import‘ keyword.

import git

The ‘git’ module inside GitPython contains classes and functions that perform git operations inside Python code.

Clone Repository

We should always remember that GitPython works with local repositories stored on our system. If we want to bring a repository (published on GitHub) to our local system, we need to use the ‘clone’ method as follows:

repo_url = 'https://github.com/github_username/repository_name'
local_path = r'C:\Users\Lenovo\Desktop\Python\Repos'
repo = git.Repo.clone_from(repo_url, local_path)

Inside your repo_url, add the URL to your repository. from Github. Second, make a folder on your system and pass the URL of the file to your local_path. Lastly, use the clone method to call all source files of the repository into your system.

Checking out a specific commit ID

Once you have initialized your ‘Repo’ object, we will use the ‘git.checkout()’ function to work with a specific commit ID.

try:
    commit_id = 'Your_commit_id'
    repo.git.checkout(commit_id)
    print("Checkout successful!")
except Exception as e:
    print(f"Error during checkout: {e}")

Here we have used try and except block to see if the checkout function works well. Add the commit_id you want to check in your repository. If GitPython can check that commit ID, it will print the message “Checkout successful’. Else it will print the error message as.

Example:

from git import Repo

repo_url = 'https://github.com/varnikarathee/DSA_Leetcode.git'
local_path = r'C:\Users\Lenovo\Desktop\Python\Repos'
repo = git.Repo.clone_from(repo_url, local_path)

try:
    commit_id = 'a745da0'
    repo.git.checkout(commit_id)
    print("Checkout successful!")
except Exception as e:
    print(f"Error during checkout: {e}")

After adding the path URLs and the commit ID. We can see the following output:

output1
output1

From the output, we can say that the ‘checkout’ command successfully tested the commit ID. This shows that GitPython is inside this file and you can perform other Git operations.

Track Commit History

We can also check out the repository’s commit history. We will use the ‘git.log’ method to track all commits and print them out in Python.

commits = repo.git.log()

By default, the commits will contain all the commit logs. To make the commit more readable, we can use the split function to print each commit in a new line.

commits = repo.git.log()
lines = commits.split('\n')
for commit in lines:
        print(commit)
output2
output2

Thus, we can see our commits are visible line by line. It provides details like the Author, Date of commit, Time of Commit, and Space occupied by the commit in the output.

Which method is used to check out a certain Git commit in GitPython?

The ‘git. checkout’ method is used to step inside a certain commit or file in the repository. Further, it allows us to perform multiple Git operations inside the commit using the GitPython module.

How to extract certain parameters from the commit history?

We can add parameters and pass them inside the git.log() function to extract certain data from commit history. For example, pass the argument like ‘–author=John Doe’ to find details of authors who made commits.

Summary

GitPython offers a more versatile approach. We can use the git module to call functions like ‘checkout()’ and ‘log()’ to track our commit history. The ‘checkout’ command tells us that GitPython is inside a certain commit and allows us to perform git operations on the commit. Likewise, the ‘log()’ function tracks history or retrieves data like author details, date, and time from the commits. We should also not forget to clone the repository from GitHub on our local system and perform the above operations.

References