How to Delete Local and Remote Branches in Git

Working with branches is a core part of using Git. Often you will want to clean up and remove local and/or remote branches. Below are examples of how to delete local and remote branches in Git.

Delete a Local Branch

1) git branch -d <branch-name>
2) git branch --delete <branch-name>

The results of these two commands are identical. These commands will delete a local branch if the branch has been merged into its upstream branch. If you want to delete a local branch that has not been merged into its upstream, you must force delete it (shown below).

Force Delete a Local Branch

1) git branch -D <branch-name>
2) git branch --delete --force <branch-name>

These commands will perform identical actions and force delete a local branch.

Delete a Remote Branch

1) git push origin :<branch-name>
2) git push origin --delete <branch-name>

The results of these two commands are identical and delete a branch from a remote repository (like GitHub).

Code Example

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">