Cao Yi

Automating the Removal of Outdated and Merged Branches in Git Repositories

Index

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.

Prerequisites

Before we dive into the implementation, make sure you have the following information:

  1. The local path to your Git repository, such as /home/sandbox/test.
  2. The name of the main branch in your repository, which is commonly ‘main’ or ‘master’.

Requirements

To achieve our goal of automating the removal of outdated and merged branches, we need to fulfill the following requirements:

  1. Identify the branches that have been merged into the main branch in the remote repository.
  2. Determine if the commit date of each merged branch is older than 1 month.
  3. Delete the outdated branches from the remote repository.

Implementation

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:

  1. Open the local Git repository.
  2. Checkout the main branch (in this case, ‘master’).
  3. Pull the latest changes from the remote repository.
  4. Find the remote branches that have been merged into the main branch.
  5. Split the branches into a list for further processing.
  6. Determine the cutoff date (1 month ago from today).
  7. Iterate over the branches and retrieve the commit date of each branch.
  8. Check if the commit date is older than the cutoff date.
  9. If the branch meets the criteria, delete it from the remote repository.

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.

Conclusion

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!