Open In App

Delete a Git Branch Locally and Remotely

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Branches in Git are an essential part of the workflow, allowing developers to work on features, bug fixes, or experiments without disturbing the main codebase. However, once a branch has served its purpose, it’s a good practice to delete it to keep the repository clean and organized.

This guide will walk you through the steps to delete a Git branch both locally and remotely.

Delete a Git Branch Locally

Git won't allow you to delete a Git branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:

Syntax

git checkout <branch-name>

Here we will check out our main branch from my test branch.

Checkout

Delete the test branch locally:

Syntax

git branch -d <branch-name>

We will delete my test branch as an example.

Delete Branch Locally

Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with --delete --force. This will forcefully delete the branch even if it hasn't been pushed or merged with the remote. the full command is:

Syntax

git branch -D <branch-name>

With this, we can successfully delete a local branch.

Delete a Git Branch Remotely

You can’t use the git branch command to delete a remote branch. Instead, you have to use the git push command with the --delete flag, followed by the name of the branch that you want to delete. You also need to specify the remote name (origin in this case) after "git push". The command is as follows:

Syntax

git push <remote-name> --delete <branch-name>

Here I will delete my test branch in my remote repository as shown below.

Delete Branch Remotely

This command will delete the branch remotely. You can also use the shorthand:

Syntax

git push <remote-name> :<branch-name>

As you can see my remote branch is no more in my GitHub repo:

Deleted From GitHub

With this, we have successfully deleted our remote branch. A common error faced by many in this step is:

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'

This means that someone has already deleted the branch that you want to delete. If this happens you can use the following command to synchronize your branch list in the local environment:

Syntax

git fetch -p

The -p flag here means "prune". After fetching the branches which no longer exist remotely will be deleted in your local working environment. 

Local Deletion vs Remote Deletion

ActionLocal DeletionRemote Deletion
Commandgit branch -d branch-namegit push origin --delete branch-name
Force Deletiongit branch -D branch-namegit push origin :branch-name
PurposeRemoves branch from local repositoryRemoves branch from remote repository
Safety-d flag is safer, prevents unintentional deletionMust ensure the branch is not needed anymore.


Suggested Quiz
5 Questions

Which command is used to delete a branch locally only if it is already merged?

  • A

    git branch -D <branch>

  • B

    git push origin --delete <branch>

  • C

    git branch -d <branch>

  • D

    git checkout <branch>

Explanation:

git branch -d safely deletes a local branch only after it has been merged. It prevents accidental data loss.

What should you do before deleting a local branch?

  • A

    Force delete immediately

  • B

    Switch to another branch using git checkout

  • C

    Delete it remotely first

  • D

    Use git merge

Explanation:

Git does not allow deleting the branch you are currently on, so you must first checkout a different branch.

Which command is used to forcefully delete a Git branch locally, even if unmerged?

  • A

    git push origin main

  • B

    git branch -d <branch>

  • C

    git branch -D <branch>

  • D

    git remote prune

Explanation:

git branch -D <branch> (or --delete --force) force-deletes a branch even if not merged.

How do you delete a branch from a remote repository?

  • A

    git branch --delete origin <branch-name>

  • B

    git push origin --delete <branch-name>

  • C

    git delete remote <branch-name>

  • D

    git remove <branch>

Explanation:

Remote branch deletion uses git push with --delete option:

git push origin --delete <branch-name>

If a branch was already deleted remotely elsewhere and causes an error during deletion, which command helps sync your local branch list?

  • A

    git pull

  • B

    git reset

  • C

    git fetch -p

  • D

    git merge

Explanation:

git fetch -p prunes references to branches that no longer exist remotely, updating local branch list.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore