DEV Community

Play Button Pause Button
Ahmad Awais ⚡️
Ahmad Awais ⚡️

Posted on

Delete a Git branch both locally and remotely [Shell aliases + functions] #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 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
}
Enter fullscreen mode Exit fullscreen mode

P.S. If you like my work, feel free to share it, like it. I am quite active on twitter you can find me cracking silly jokes there, maybe follow me on Twitter to connect and subscribe to my YouTube channel →
Peace! ✌️

🗣️ Comment below if you didn't get something.

Top comments (5)

Collapse
 
itsdarrylnorris profile image
Darryl Norris

This is very helpful rather than Googling every time on how to delete a local and remote branch. Have you considered making a project with all your shell scripts? Something we can download use and contribute?

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Super glad you liked it. I have started this #OneDevMinute series for this exact reason — share all of my workflows in mini chunks.

As I mentioned the 📨 Newsletter + site coming soon part where you'll be able to sign up.

For now, you can sub to my Twitter or YouTube — I actually made a funny gif for that, let me find that one.

Here it goes 🤣

Can't share it all, since it's not organized and has lots of moving parts + personal data in it.
I'm doing that with this series.

Peace! ✌️

Collapse
 
math2001 profile image
Mathieu PATUREL

Nice tip!

Any reason for using force to delete local branch though (git branch -D "$1", the d is majuscule)?

I think it'd be safer to use git branch -d "$1"

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️ • Edited

Thanks.

I think that's because it avoids the message of the branch not being merged?

Collapse
 
math2001 profile image
Mathieu PATUREL

Yes, so that means you lose your code (hence the force).