As software developers, we often work with branches in our Git repositories. Once a branch has been successfully merged into the main branch (commonly referred to as ‘main’ or ‘master’), it is generally considered best practice to remove the original branch. However, it’s easy to forget about these merged branches, leading to clutter and potential confusion in the repository.
In this article, we will explore how to automate the process of detecting and deleting outdated branches that have been merged into the main branch using Python. By following the steps outlined below, you’ll be able to effortlessly clean up your Git repository and maintain a streamlined codebase.
Before we dive into the implementation, make sure you have the following information:
/home/sandbox/test
.To achieve our goal of automating the removal of outdated and merged branches, we need to fulfill the following requirements:
To accomplish these requirements, we will utilize the powerful GitPython library in Python. GitPython provides a convenient and intuitive way to interact with Git repositories programmatically.
Let’s proceed with implementing the script:
#!/usr/bin/env python3
import datetime
import git
# Local repository path
repo_path = '/home/sandbox/test'
# Open the repository
repo = git.Repo(repo_path)
# Checkout the main branch
repo.git.checkout('master')
# Pull the latest changes from the remote repository
repo.remotes.origin.pull()
# Get the remote branches merged into the main branch
merged_branches = repo.git.branch('--remotes', '--merged', 'origin/master')
# Split the branches into a list
branches = merged_branches.split('\n')
# Exclude the main branch
branches = [branch.strip() for branch in branches if 'origin/master' not in branch]
# # Determine the cutoff date (1 month ago from today)
cutoff_date = datetime.datetime.now() - datetime.timedelta(days=30)
cutoff_date = cutoff_date.replace(tzinfo=None)
for branch in branches:
# Retrieve the commit date of the branch
commit_date = repo.git.log('-1', '--format=%cd', branch)
commit_date = datetime.datetime.strptime(commit_date, '%a %b %d %H:%M:%S %Y %z')
commit_date = commit_date.replace(tzinfo=None)
# Check if the commit date is more than 1 month ago
if commit_date < cutoff_date:
repo.git.push('origin', '--delete', branch)
print(f"Deleted branch '{branch}'")
Make sure you have the GitPython library installed (pip install GitPython
) before running the script.
The provided script will perform the following steps:
By executing this script, you will be able to automatically detect and remove outdated branches that have been merged into the main branch in your remote repository.
Cleaning up outdated and merged branches is an essential maintenance task in Git repositories. With the help of GitPython, we have built a Python script that automates this process. By periodically running this script, you can ensure that your repository remains organized and clutter-free.
Remember to exercise caution when deleting branches, as this action is irreversible. Always double-check and verify before removing any branches from your remote repository.
Happy coding!