Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Created August 31, 2011 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroasterisk/1183610 to your computer and use it in GitHub Desktop.
Save zeroasterisk/1183610 to your computer and use it in GitHub Desktop.
git-rmbranch script to facilitate easy branch removal, locally and on origin
#!/bin/bash
# -----------------------------------------------------------
# @requires confirm.sh in the same location as this script
# @link http://wuhrr.wordpress.com/2010/01/13/adding-confirmation-to-bash/
# -----------------------------------------------------------
# usage:
# git rmbranch branchname [branchname ...]
# will force delete N number of branches locally and on origin
SCRIPT="$(readlink -nf ${0#./})"
SCRIPTNAME="${0##*/}"
SCRIPTBASE="${SCRIPT%/*}"
source ${SCRIPTBASE}/confirm.sh
for branchname in "$@"
do
if [[ "$branchname" =~ ^(master|test|dev|qa|prod)$ ]]
then
echo "::Restricted Branch: $branchname -- no action taken::"
else
confirm "Delete $branchname (y/n)"
if [ $? -eq 0 ]
then
echo "::Deleting branch locally::$branchname"
git branch -D "$branchname" && echo "^^done"
echo "::Deleting branch on origin::$branchname"
git push origin :"$branchname" && echo "^^done"
fi
fi
done
exit 0
@zeroasterisk
Copy link
Author

Here's an example of it being used.. note that the branch "temp-0000-testingD" wasn't on the origin, so git returned an error, but the script just keeps on processing like normal.


zero@am /ws/www/ahm/sp/app> git rmbranch temp-0000-testingA temp-0000-testingB temp-0000-testingC temp-0000-testingD

Delete temp-0000-testingA (y/n) y
::Deleting branch locally::temp-0000-testingA
Deleted branch temp-0000-testingA (was 8044c54).
::done::
::Deleting branch on origin::temp-0000-testingA
To git@github.com:xxxxxxxx/SP.git
 - [deleted]         temp-0000-testingA
::done::
Delete temp-0000-testingB (y/n) y
::Deleting branch locally::temp-0000-testingB
Deleted branch temp-0000-testingB (was 8044c54).
::done::
::Deleting branch on origin::temp-0000-testingB
To git@github.com:xxxxxxxx/SP.git
 - [deleted]         temp-0000-testingB
::done::
Delete temp-0000-testingC (y/n) y
::Deleting branch locally::temp-0000-testingC
Deleted branch temp-0000-testingC (was 8044c54).
::done::
::Deleting branch on origin::temp-0000-testingC
To git@github.com:xxxxxxxx/SP.git
 - [deleted]         temp-0000-testingC
::done::
Delete temp-0000-testingD (y/n) y
::Deleting branch locally::temp-0000-testingD
error: Cannot delete the branch 'temp-0000-testingD' which you are currently on.
::Deleting branch on origin::temp-0000-testingD
error: unable to push to unqualified destination: temp-0000-testingD
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@github.com:xxxxxxxx/SP.git'

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