Skip to content

Instantly share code, notes, and snippets.

@demirtasdurmus
Last active July 31, 2023 10:57
Show Gist options
  • Select an option

  • Save demirtasdurmus/c90ee594d44a2b4126dfd716ea55e4c0 to your computer and use it in GitHub Desktop.

Select an option

Save demirtasdurmus/c90ee594d44a2b4126dfd716ea55e4c0 to your computer and use it in GitHub Desktop.
A shell script to delete all local git branches except the one specified while calling it. It also checks if there are any unpushed changes and prompts user to choose the appropriate action including either aborting the operation or pushing the changes
#! /bin/bash
# declare colors
Red=$'\e[1;31m'
Green=$'\e[1;32m'
Blue=$'\e[1;34m'
Yellow=$'\e[1;33m'
# exit when any command fails
set -e
## declare the passed branch name
passedBranchName=""
## declare the branches except the one passed
BRANCHES=""
## check if a branch name is passed via a function
function isBranchNamePassed() {
if [ -z "$1" ]; then
echo "${Red}Please pass a branch name!"
return 1
else
passedBranchName="$1"
fi
}
isBranchNamePassed "$1"
## check if the passed branch exists
function doesBranchExist() {
if ! git show-ref --verify --quiet refs/heads/"$1"; then
echo "${Red}Branch $Yellow$1 ${Red}does not exist!"
return 1
fi
}
doesBranchExist "$passedBranchName"
## check if the passed branch is the current branch
## switch to the passed branch if it is not the current branch
function moveToPassedBrachIfNotAlreadyIn() {
if [ "$(git symbolic-ref --short -q HEAD)" != "$1" ]; then
echo "${Blue}Moving move to branch $Yellow$1!"
git checkout "$1" --quiet
fi
}
moveToPassedBrachIfNotAlreadyIn "$passedBranchName"
## check if there are other local branches except the one passed
function isThereAnyLocalBranchesExceptTheOnePassed() {
if [ -z "$(git branch | grep -v "$1" | xargs)" ]; then
echo "${Red}No local branches to delete except $Yellow$1"
return 1
else
BRANCHES=$(git branch | grep -v "$1" | xargs)
fi
}
isThereAnyLocalBranchesExceptTheOnePassed "$passedBranchName"
## loop through the branches and
## check if there are any unpushed changes
## prompt the user to push them if there are any
## then delete the branch
function askDeletionOfUnpushedBranch() {
## prompt the user if he/she wants to abort the operation
## for this specific branch
echo -n "Abort the operation for this specific branch? (y/n) "
read -r answerA </dev/tty # read from keyboard
## exit if the answer is not yes or continue otherwise
if [ "$answerA" != "${answerA#[Yy]}" ]; then
echo "${Green}Aborted..."
return
else
echo "${Blue}Continuing to delete the brach..."
# delete the specific branch as all the checks are passed
git branch -D "$1" --quiet
# show a message ensuring the branch is deleted
echo "***** ${Green}Deleted the $Yellow$1 *****"
fi
}
function checkAndDeleteBranches() {
for currentBranch in $1; do
echo "${Blue}Checking availabity for $Yellow$currentBranch"
commits=$(git log "$currentBranch" --not --remotes --simplify-by-decoration --decorate --oneline)
## check if there are any unpushed changes for this specific branch
if [ -z "$commits" ]; then
# delete the specific branch as all the checks are passed
git branch -D "$currentBranch" --quiet
# show a message ensuring the branch is deleted
echo "***** ${Green}Deleted the $Yellow$currentBranch *****"
else
echo "${Red}There are unpushed changes in branch $Yellow$currentBranch"
echo "${Yellow}****************************************************"
echo "${Blue}$commits"
echo "${Yellow}****************************************************"
echo -n "${Red}Push them? (y/n) "
read -r answerP </dev/tty # read from keyboard
## exit if the answer is not yes or continue otherwise
if [ "$answerP" != "${answerP#[Yy]}" ]; then
echo "${Blue}Pushing changes..."
git push origin "$currentBranch" --quiet
echo "${Green}Pushed changes for branch $Yellow$currentBranch"
else
askDeletionOfUnpushedBranch "$currentBranch"
continue
fi
fi
done
}
checkAndDeleteBranches "$BRANCHES"
@demirtasdurmus
Copy link
Author

  • Add it to a location with appropriate rights.
  • Provide an alias in your .zshrc or .bashprofile file.
  • Call it inside your repository with the argument (the branch name you want to keep and delete all others)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment