Skip to content

Instantly share code, notes, and snippets.

@ahmadawais ahmadawais/OneDevMinute.md
Last active Oct 18, 2018

Embed
What would you like to do?
[OneDevMinute]: Delete a Git branch both locally and remotely [Shell aliases + functions]
title category date
Delete a Git branch both locally and remotely [Shell aliases + functions]
Shell, Git
2018-09-24

OneDevMinute

Deleting a git branch locally is pretty easy, just do a git branch -D branch_name and you're done. But when you have pushed that branch to remote, it becomes a bit more complex. You can't just delete the local branch and do a git push. Instead you have to do a git push origin --delete branch_name to delete it from the remote.

All of that becomes too much for a simple task of deleting a git branch. Which is why, just copy/paste the following in your .zshrc/.bashrc file and you'll be able to use gcm, gbn, gbdel commands to do all of this in a jiffy.

# FILE: .zshrc/.bashrc

################################################
# 🔥 #OneDevMinute
#
# Daily one minute developer tips.
# Ahmad Awais (https://twitter.com/MrAhmadAwais)
################################################

# Checkout to master
alias gcm="git checkout master"

# Create new branch and checkout.
alias gbn='git checkout -b'

# Remove git branch both locally and remotely.
# Usage: gbdel branch_name
function gbdel {
	# Branch name present?
	if [[ -z "$1" ]]; then
		echo "\n🤔 Oops… you forgot to provide the branch name"
		echo "👉 E.g. gbdel branch_name\n"
	else
		# Start deleteing.
		echo "\n⏳ Deleting…\n"
		git branch -D "$1" # Local delete.
		git push origin --delete "$1" # Remote delete.
		echo "\n✅ Git branch $1 was deleted from local and remote.\n"
	fi
}

P.S. If you like my work, feel free to share it, like it, and subscribe. Peace! ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.