How I Streamlined My Git Workflow: A Personal Journey to Repository Zen

How I Streamlined My Git Workflow: A Personal Journey to Repository Zen
Photo by Yancy Min / Unsplash

As a Software Developer, managing a clean and efficient Git repository is not just a part of my job—it's a necessity for maintaining sanity. Over time, I've seen firsthand how quickly feature and hotfix branches can pile up, turning what was once a neatly organized repository into a daunting labyrinth of stale branches. It was time for a change, so I crafted a solution that not only simplified my workflow but also brought a sense of zen to our repository. Here's my story and the script that made it all happen.

The Birth of the Cleanup Script

The clutter in our repository was becoming a silent productivity killer. Navigating through branches felt like searching for a needle in a haystack, and manually checking and deleting old branches was eating up valuable time. That's when I decided to automate the process.

The script I created is straightforward yet effective. It begins with a simple prompt for the repository path, ensuring it's valid before proceeding. It then dives into the heart of the repository, fetching all branches and pruning any that no longer exist on the remote.

Here's my script:

#!/bin/bash

echo "Enter the path to your Git repository:"
read repo_path

# Validate the input path
if [[ -z "$repo_path" ]] || [[ ! -d "$repo_path" ]]; then
    echo "Invalid path. Exiting."
    exit 1
fi

# Navigate to the Git repository directory
cd "$repo_path"

# Fetch all branches from the remote and prune any deleted branches
git fetch --prune origin

# Loop through all branches that are not 'master'
for branch in $(git branch -r | grep -E 'origin/(hotfix/|feature/)' | sed 's/origin\///'); do
    if [[ "$branch" == "master" ]]; then
        continue
    fi

    echo "Checking $branch..."

    # Check out the branch to ensure it's available locally for comparison
    git checkout -B "$branch" "origin/$branch" --quiet

    # If the branch is fully merged into master
    if ! git cherry -v master "$branch" | grep -q '^+'; then
        echo "$branch is fully merged into master. Deleting..."
        # Delete branch locally and remotely
        git push origin --delete "$branch"
        git branch -D "$branch"
    else
        echo "$branch is NOT fully merged into master."
        # Ask what to do with the branch using shorthand options
        echo "What do you want to do with $branch? [d]elete/[k]eep/[s]kip"
        read action

        case "$action" in
        d)
            # Delete branch locally and remotely
            git push origin --delete "$branch"
            git branch -D "$branch"
            echo "$branch deleted remotely and locally."
            ;;
        k)
            echo "Keeping $branch."
            ;;
        s)
            echo "Skipping $branch."
            ;;
        *)
            echo "Invalid option. Skipping $branch."
            ;;
        esac
    fi
done

# Checkout the main branch (or your default branch) when done
git checkout master --quiet

echo "Operation completed."

The Heart of the Operation

The core functionality loops through branches, specifically targeting those labeled as feature/ or hotfix/, while carefully avoiding the master branch. It checks whether each branch is fully merged into master. If it is, the script gracefully deletes it both locally and remotely, clearing the way for a cleaner workspace. For branches not fully merged, it offers a choice: delete, keep, or skip, putting the decision-making power back in my hands.

Why This Script Was a Game-Changer for Me

Efficiency at Its Best: What used to be a tedious, manual task transformed into a streamlined process. This script saves me hours, allowing me to focus more on development and less on repository maintenance.

Reduced Human Error: Automating this process minimized the risk of accidentally deleting important branches, a mistake I'm not proud to admit I've made in the past.

A Cleaner, Happier Repository: Regularly purging merged branches has kept our repository neat and manageable, making it easier for the entire team to find and work on what's important.

How You Can Implement This Script

  1. Setup: If you're working in a Unix-like environment with Git installed, you're already halfway there. Copy the script into a file, give it execution rights with chmod +x yourscriptname.sh, and run it with ./yourscriptname.sh.
  2. Customization: Feel free to tweak the script to fit your workflow better. Adjust branch naming conventions or modify its behavior to suit your team's needs.

In Conclusion

Creating this script was a small step that led to a significant improvement in my daily work life and our team's productivity. It reminded me that sometimes, the best solutions come from addressing the simplest problems.

I hope sharing my journey inspires you to look at your workflow and find ways to automate the mundane, giving you more time to focus on what truly matters. After all, a clean repository is not just about aesthetics; it's about efficiency, safety, and peace of mind.

Subscribe to codingwithalex

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe