Open In App

How To Clean Old Remote Git Branches?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Over the time, remote Git repositories can have outdated or unused branches that clutter the project history and make it harder to navigate. Cleaning up these branches is important for maintaining a well-organized repository. In this article, we will learn how to clean old remote Git branches.

Approach 1: Manual Deletion

This involves using Git commands to list, review, and delete branches individually. It provides control but can be time-consuming for repositories with many branches.

Step 1: List Remote Branches

Use the following command to view all remote branches in the repository:

# List all remote branches
git branch -r

Output

Screenshot-2024-08-11-013955
Listing Remote Branches

Step 2: Identify Stale Branches

Carefully examine the list of branches and identify those that are no longer needed. Consider factors like:

  • Merged branches: Branches that have been merged into the main development branch can usually be safely deleted.
  • Inactive branches: Branches with no recent commits might be abandoned or no longer relevant.
  • Feature branches: Branches created for specific features that are no longer under development can be removed.

Step 3: Delete Remote Branches

After identification of the branches that need to be deleted, use the following command:

# Delete a specific remote branch
git push origin --delete <branch_name> # Replace <branch_name> with the actual branch name

#For example:
git push origin --delete newbranch

Output

Screenshot-2024-08-11-014146
Deleting Remote Branches

Approach 2: Automated Scripts

We can use scripts or tools to automate the process of finding and deleting branches based on specific criteria, such as age or lack of recent commits. This approach is more efficient for large repositories.

Step 1: Choose a Script or Tool

Several scripts and tools are available for automating the cleanup process. Some popular options include:

  • git-cleanup: A script that provides various options for finding and deleting branches based on age, merged status, and other criteria.
  • Git-Branch Cleaner: A tool with a user-friendly interface for selecting and deleting branches.

Step 2: Configure and Run the Script

Follow the instructions for the chosen script or tool to configure any necessary parameters, such as the age threshold for deleting branches. Below is an example of a simple Bash script to automate branch cleanup:

#!/bin/bash

# Fetch the latest state of remote branches and prune deleted branches
git fetch --prune

# Get the current date minus 30 days
THRESHOLD_DATE=$(date -d '30 days ago' +%s)

# Loop through remote branches
for branch in $(git branch -r | grep -v '\->'); do
# Get the last commit date of the branch
LAST_COMMIT_DATE=$(git log -1 --format=%ct "${branch#origin/}")

# Check if the branch is older than the threshold
if [[ $LAST_COMMIT_DATE -lt $THRESHOLD_DATE ]]; then
# Delete the remote branch if it's older than 30 days
echo "Deleting branch: ${branch#origin/}" # Inform about the deletion
git push origin --delete "${branch#origin/}" # Delete the branch
fi
done

Next Article
Article Tags :

Similar Reads