Last active
December 14, 2015 12:29
-
-
Save sdthornton/5086489 to your computer and use it in GitHub Desktop.
Deletes a git branch both locally and remotely.
To use simply type:
delete 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
# Easily delete branches remotely and locally | |
# Simple type: | |
# delete feature-branch-name | |
function delete_branch() { | |
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 | |
} | |
function delete() { | |
branch=$1 | |
if [[ -z "$branch" ]]; then | |
echo -e "\033[0;31mNo branch given. Please specify a branch to delete:" | |
tput sgr0 | |
echo "(Type \"esc\" to stop branch deletion)" | |
while true; do | |
read -p "> " input | |
case $input in | |
esc | Esc | ESC ) break;; | |
* ) delete_branch $input; break;; | |
esac | |
done | |
else | |
delete_branch $branch | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Although
git branch -d branch-name
already tells you that you have unpushed commits, it doesn't allow you to delete the branch at that moment (without going back and using-D
).Accordingly, in this I've set up a way to check against remote to see if there are unpushed commits and warns you, but gives you the option of still deleting the branch.