Last active
December 25, 2024 11:20
-
-
Save claudioc/c0b7b1bf3eab18457e36 to your computer and use it in GitHub Desktop.
Safely delete a branch, remotely and locally. I use it as ~/bin/git-purge and use it with `git purge <branch-name>`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "${1}" = "" ]; then | |
echo "Please specify a branch name to delete" | |
exit 1 | |
fi | |
current=$(git symbolic-ref HEAD | sed 's/refs\/heads\///') | |
if [ "master" != "${current}" ]; then | |
echo Please move to the master branch first | |
exit 1 | |
fi | |
upstream=$(git for-each-ref --format='%(upstream:short)' refs/heads/${1}) | |
if [ "" == "${upstream}" ]; then | |
echo "Sorry, upstream not found for that branch; I rather not continue." | |
echo Delete it locally with \`git branch -D ${1}\` | |
echo Delete it remotely with \`git push \<your remote\> --delete ${1}\` | |
exit 1 | |
fi | |
branch=($(echo ${upstream} | tr "/" "\n")) | |
ismerged=$(git branch --merged | grep ${branch[1]}) | |
if [ "" == "${ismerged}" ]; then | |
echo ====================================== | |
echo WARNING: The branch is not yet merged! | |
echo ====================================== | |
fi | |
echo "Going to delete the branch \`${branch[1]}\` both locally and from the remote \`${branch[0]}\`" | |
echo | |
echo "Is that what you want? Say 'yes', please" | |
read zot | |
if [ "yes" == "${zot}" ]; then | |
git push ${branch[0]} --delete ${1} | |
git branch -D ${1} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment