Delete a git branch locally and remotely in 1 minute (2022)

Written by Shan Shah
Updated on September 16, 2022
4 mins read
how to delete a git branch locally or remotely

Are you working on a git repository and want to delete a git branch locally?

Do you need to delete a git branch remotely but don't know how to do that?

No problem! This simple tutorial will teach you how to do both in just minutes.

First, you will learn how to delete a local git branch locally.

Then, I will demonstrate how to delete a git branch remotely using the git push command.

Finally, you will learn the benefits of using these commands and some best practices for remote deletion.

So once you've mastered this simple tutorial, you'll be able to delete any git branch with ease!

Note:
Suppose you want to delete any git branch, whether locally or remotely. You will always need to checkout from the branch you want to delete.

Case Statement

Let's say a repository has a master or main branch.

Once the main features are complete, all developers need to merge their branch into the master branch.

You are working on a feature-register branch.

You have completed the feature and now want to merge it.

Open a command line tool and run:

git checkout master

Now, you have moved into the master branch and want to delete the feature-register branch.

Tip: Always make sure to have a meaningful branch name of the git repo.

How to delete a git branch locally?

how to delete a git branch locally

Git branch deletion is an essential task when working with git repositories.

Here are the steps to delete a git branch locally.

git branch --delete feature-register

OR

git branch -d feature-register (Shorter version)

-d is an alias of --delete

When your local branches are not merged with the remote, and you try to delete them, git will throw an error that the specific branch is not merged. In that case, you can delete unmerged branches forcefully by using -D (--delete-force)

git branch -D feature-register

How to delete a git branch Remotely?

how to delete a git branch remotely

Git branches can be a pain to manage - especially when you want to delete them remotely.

Luckily, git has a nifty command for the job - git push.

What about when you push the branch to a remote hub like GitHub or Bitbucket?

And now you want to delete that specific remote branch.

At this point, you can utilize the commands mentioned below:

git push --delete origin feature-XYZ

OR

git push -d origin feature-XYZ

FAQs:

how to delete a git branch locally and remotely

Is it safe to delete the local branch in git?

No. To delete a git branch locally, you must first merge the feature branch into another one and then destroy the original local branch.

Should I delete merged git branches?

Merged git branches should be deleted if there is no need for them. It can help speed up git operations.

Is there any way to delete a local branch without merging it into master first?

Sometimes, you complete a feature that never merges because there is no longer need for that created feature.

For this case, none of the above commands will work. It will throw an error like:

"error: The branch 'feature-XYZ is not fully merged."

If you are sure you want to delete it, run 'git branch -D feature-XYZ.'

If you want to delete an unmerged branch forcefully, you will have to use:

git branch -D feature-XYZ

How do I delete old local branches in git?

You can delete all the local branches at once by using following command:

git branch --merged | grep -v \* | xargs git branch -D

Note: This will delete the master branch as well, if you are inside other then master branch.

How to delete all branches except master?

If you want to delete all branches except main or master branch. You can use following command:

git branch | grep -v "master" | xargs git branch -D

OR

git branch | grep -v "main" | xargs git branch -D

That's it! In a nutshell, you learned:

  • How to delete a git branch locally or remotely
  • How to delete all the branches except specific branch like master
  • How to delete the old local branches

Conclusion

Git branch deletion is an important process that helps to keep your codebase organized and manageable. This tutorial taught you how to delete a git branch locally or remotely. Not only that, but you have also learned about deleting the unmerged branches forcefully.

You can also look into the git branch deletion process from stackoverflow.

Looking for this article? : 10 Ways to earn money online as a Web Developer

You can now tweet this guide with your followers by using CLICK TO TWEET

How to delete a git branch locally and remotely? Click To Tweet
Project Based Tutorials
Tips & Tricks
Modern Technologies
Real-World Challenges
Creative Content
Build-Apps from Scratch
Web Development Content
17+ Videos Published
Coder Champ © 2021 - 
Privacy Policy
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram