Are your local Git branches multiplying like rabbits, leaving you overwhelmed and disorganized? Do you find yourself drowning in a sea of outdated branches, desperately seeking a lifeline? Fear not, for the "Branch Broomstick" is here to rescue you from the depths of branch chaos!
With just a flick of the -f
switch, this trusty script will sweep away those pesky, forgotten branches that no longer grace the remote. Say goodbye to branch clutter and hello to a cleaner, more serene repository. Dive into the instructions below and let the tidying commence! 🚀✨
- Deletes local branches that are not present on the remote.
- Optionally force deletes branches using the
-f
or--force
flag.
Follow these steps to set up the script on your computer:
-
Open a terminal.
-
Create a directory for your scripts if it doesn't already exist:
mkdir -p ~/bin
-
Save the script to a file named
clean-local-branches.sh
in the~/bin
directory:nano ~/bin/clean-local-branches.sh
-
Copy and paste the following script into the file:
#!/bin/bash # Default delete command delete_command="git branch -d" # Check for -f or --force flag while [[ "$#" -gt 0 ]]; do case $1 in -f|--force) delete_command="git branch -D"; shift ;; *) echo "Unknown parameter passed: $1"; exit 1 ;; esac shift done # Fetch the latest state of the remote branches git fetch --prune # Get a list of local branches local_branches=$(git branch --format='%(refname:short)') # Get a list of remote branches remote_branches=$(git branch -r --format='%(refname:short)' | sed 's/origin\///') # Loop through each local branch for branch in $local_branches; do # Check if the branch exists on the remote if ! echo "$remote_branches" | grep -q "^$branch$"; then # If not, delete the local branch echo "Deleting local branch: $branch" $delete_command "$branch" fi done
-
Save and exit the editor (in
nano
, pressCTRL + X
, thenY
, andEnter
).
Run the following command to make the script executable:
chmod +x ~/bin/clean-local-branches.sh
Ensure that the ~/bin
directory is included in your PATH
. Add the following line to your ~/.bashrc
or ~/.zshrc
file:
export PATH="$HOME/bin:$PATH"
Reload your shell configuration:
-
For Bash:
source ~/.bashrc
-
For Zsh:
source ~/.zshrc
You can now run the script from any directory:
-
Normal Deletion: Deletes branches that are fully merged.
clean-local-branches.sh
-
Force Deletion: Forcefully deletes branches, regardless of their merge status.
clean-local-branches.sh -f
Or:
clean-local-branches.sh --force
- Safety: Always ensure that you have pushed any important changes to the remote before running this script, as it will permanently delete local branches that do not exist on the remote.
- Remote Name: The script assumes the default remote name is
origin
. Adjust the script if your remote has a different name.
Feel free to share this script with others to help them manage their local Git branches more efficiently!
Note: The author of this script takes no responsibility for any unpushed changes that may be lost during the branch cleaning process. Please ensure that all important changes are safely pushed to the remote before using this script. Happy cleaning!