Delete Git Branch from Local and Remote (With EXAMPLE)

You can delete the local git branch with the git command "git branch -d <branch-name>".

git branch -d <branch-name>

 

Now let's see in detail if you get some error while deleting git local branch or you want to delete the branch on remote as well.

Delete LOCAL branch in Git

Deleting a single git branch on local can be done with a single command.

git branch -d my-branch

-d flag in the above command represents --delete and will delete the local branch if you don't have any unmerged commits in it.

Delete local branch ERROR

If you have unmerged commits in the branch and you are trying to delete then it will throw an error saying your branch is not fully merged.

$ git branch -d my-branch
error: The branch 'my-branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D my-branch'.

As mentioned in the output of the command we ran, you need to use the "-D" option to delete the local branch which is not fully merged. "-D" option is the combination of "-delete" and "-force" to forcefully delete the local branch.

But be careful with this as you might lose data.

git branch -D my-branch

Delete REMOTE Branch in Git

To delete a branch from the remote you need to use the "git push" command with the "--delete" flag which will delete it from the remote.

git push origin --delete my-branch

Should you delete Git branches?

There could be many reasons that you need to delete a branch in git.

Either you are deleting them because you need to clean up the git repository or you need different changes in a remote branch and for that, you need to delete it first.

Conclusion

One of the biggest things to remember while deleting a git branch from local and remote is that both are maintained differently.

Deleting the local branch will not have any effect on the remote branch or vice versa. Both of them need to be deleted separately as represented above.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
3 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.