Sign Up

Sign In

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Please make a payment to be able to ask a question.

Pay to ask a question

Please make a payment to be able to add post.

Pay to add a post

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Step-by-Step Guide To Delete a Git Branch Both Locally and Remotely

You’ll learn how to delete a Git branch locally and remotely in this article.

TL;DR version

// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

When to Delete branches

Different branches are common in Git repositories. They allow you to work on different features and fixes while isolating the new code from the main repository.

For the main codebase, repos have a main branch, and developers create other branches to work on different features.

Often, it is recommended to delete a branch once a feature has been completed.

Deleting a branch LOCALLY

To delete the branch you are currently on, you must checkout a branch that you are NOT deleting. For example: git checkout main.

Delete a branch with git branch -d <branch>.

For example: git branch -d fix/authentication

By default, the -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you wish to force the branch to be deleted regardless of whether it has been pushed or merged yet.

The branch is now deleted locally.

Deleting a branch REMOTELY

The command to delete a branch remotely is git push <remote> –delete <branch>.

git push origin –delete fix/authentication, for example

The branch is now deleted remotely.

The following command can also be used to delete a branch remotely: git push <remote> :<branch>

Git push origin :fix/authentication, for example

This error may indicate that someone else has already deleted the branch.

error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'

Try to synchronize your branch list using:

git fetch -p

With the -p flag, branches that no longer exist on the remote are pruned after fetching.

We have over 8,000 no-nonsense tutorials like this one available for free, without ads. Tell your friends about us! 😉

If this article was helpful, tweet it.

FreeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers.

Related Posts

Leave a comment