Skip to content

Instantly share code, notes, and snippets.

@sdthornton
Created January 20, 2015 20:42
Show Gist options
  • Save sdthornton/ef173ab2cec55166b556 to your computer and use it in GitHub Desktop.
Save sdthornton/ef173ab2cec55166b556 to your computer and use it in GitHub Desktop.
Delete a branch locally and from Github
function delete_from_github() {
log=$(git log origin/$1..$1 --oneline)
if [[ -z "${log}" ]]; then
git push origin :$1
git branch -D $1
elif [[ "${log}" == *"path not in the working tree"* ]]; then
echo -e "\033[0;31mIt doesn't appear that your branch exists remotely."
tput sgr0
while true; do
read -p "Are you sure you want to delete this branch? [y/n] `echo $'\n> '`" yn
case $yn in
[Yy]* ) git push origin :$1; git branch -D $1; break;;
[Nn]* ) break;;
* ) echo "Please answer Yes or No.";;
esac
done
else
echo -e "\033[0;31mYour local branch: $1 has commits that have not been pushed:"
echo -e "\033[0;33m${log}"
tput sgr0
while true; do
read -p "Are you sure you want to delete this branch? [y/n] `echo $'\n> '`" yn
case $yn in
[Yy]* ) git push origin :$1; git branch -D $1; break;;
[Nn]* ) break;;
* ) echo "Please answer Yes or No.";;
esac
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment