Blog.

How to delete a branch in Git

Cover Image for How to delete a branch in Git

Deleting a branch in git is done either by running git branch -d <branch-name> in case of local branch or...

Why you can't delete both branches with one command?

Local branch and remote branch have nothing to do with each other, they are different snapshots.

Delete a local branch

You can't delete a branch if you're checked out that branch. You will see this error: Cannot delete branch 'branch-name' checked out at 'some-location'. To fix this, you will have to switch to a different branch.

After switching, to delete a local branch use the following command:

git branch -d <branch-name>

If the above git command gives you an error The branch 'branch-name' is not fully merged you can force delete it using -D instead of -d, as below:

git branch -D <branch-name>
💡
-d is shortcut for —-delete and it deletes a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream-to.
💡
-D is shortcut for --delete --force

Delete a remote branch

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

In most cases <remote-name> is origin, case in which you'll have something like:

git push origin -d <branch-name>

# Example
git push origin -d release

Daniel Turuș

@danielturus

Hi! My name is Daniel and I am a Full-stack JavaScript developer.

If you like my material, please consider following me on Twitter to get notified when new posts are published, ask me a question and stay in touch.