#OneDevMinute with Ahmad Awais

Delete a Git branch both locally and remotely [Shell aliases + functions]

4 months ago In Shell, Git

Deleting a git branch locally is pretty easy, just do a git branch -D branch_name and you're done. But when you have already pushed that branch to remote, it becomes a bit more complex. You can't just delete the local branch and do a git push to be done with it. 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! ✌️