Effortlessly Clean Up Your Local Git Branches with the gclean Function

Hey there, fellow Git enthusiasts! 👋 Have you ever noticed how quickly local branches pile up as you and your team merge and delete branches on the remote? If your local Git feels a bit cluttered with these outdated references, it's time for a spring cleaning! Let's introduce a little helper to your shell: the gclean function. It's here to sweep away those stale local branches with ease.

Why Embrace the gclean Magic? 🪄

In the bustling world of collaborative projects, branches come and go like seasons. While remote branches head to the big branch graveyard in the sky, their local counterparts tend to stick around, gathering digital dust. Without regular cleanup, you're left navigating through a ghost town of branches. That's where gclean comes to the rescue, automating the purge of these spectral branches from your local repository.

Setting Up gclean in Your Shell 🛠

Adding gclean to your digital toolbox is a breeze. Whether you’re cuddling up with Bash or Zsh, or even if you're a Fish shell fan, I've got you covered. Here's how you get started:

For Bash and Zsh Users:

  1. Open Your Shell Configuration File:
    Bash folks, head over to .bashrc. Zsh users, it’s .zshrc. Pop open your favored editor. For example:
    nano ~/.zshrc
  2. Let's Add gclean:
    Copy the magic spell below and paste it into your config file:
    gclean() {
      git fetch --prune
      local branches=$(git branch -vv | awk '/: gone]/{print $1}')
      if [ -z "$branches" ]; then
          echo "All clean! No ghost branches here."
      else
          echo "$branches" | xargs git branch -d
          echo "Poof! Gone are the stale branches!"
      fi
    }
    
  3. Seal the Deal:
    Save your changes and bid farewell to your editor.
  4. Wake Up gclean:
    Bring gclean to life in your current session by sourcing your config file:
    source ~/.zshrc
    
    Or, for Bash buddies:
    source ~/.bashrc
    
    A quicker way? Close your terminal and dive into a fresh one.

🐠 For the Fish Shell Admirers:

Fish folks, you're not left out. Here’s a slightly different chant for Fish's syntax:

  1. Dive into Your Fish Config:
    Journey to ~/.config/fish/config.fish with your editor.
  2. Summon gclean:
    Paste this enchantment:
    function gclean
      git fetch --prune
      set branches (git branch -vv | string match -r '.*: gone].*' | string match -r '\S+')
      if count $branches > 0
        echo $branches | xargs git branch -d
        echo "Poof! Gone are the stale branches!"
      else
        echo "All clean! No ghost branches here."
      end
    end
    
  3. Embrace the Changes:
    Save your masterpiece.

How to Enjoy gclean 🍹

With gclean now part of your command repertoire, bid farewell to those lingering branches with just a simple command:

gclean

Stand back as gclean tidies up your local repository, bidding adieu to branches that have outstayed their welcome. No more navigating a maze of former branches—a "Poof! Gone!" and your workspace is as tidy as can be.

Until next time, happy coding, and enjoy the cleanliness! 🌟