Skip to content
Home » How to delete a Git branch locally and remotely

How to delete a Git branch locally and remotely

To delete a Git branch locally and remotely follow any one of the below commands with the appropriate Git version currently installed in your machine.

Delete Remote Branch

Using the --delete option in git push, you can delete a remote branch. In the below code we are considering the remote name as origin (because it is used in most cases).

Syntax:

git push origin --delete <branch>  # Git version 1.7.0 or newer
git push origin -d <branch>        # Alias in the Shorter version (Git 1.7.0 or newer)
git push origin :<branch>          # older Git versions below 1.7.0

Example : If you want to remove your codefix branch from the server, run the below command:

git push origin --delete codefix

Delete Local Branch

To delete the Local Git Branch, use any one of the below command which bests suites for your need.

git branch -d <branch_name>
git branch -D <branch_name>

Here -d and -D works as follows:

  1. The -d option is an alias for --delete, which deletes the branch only if it has previously been fully merged in its upstream branch.
  2. The -D flag is an alias for --delete with --force, which deletes the branch regardless of its merged status.
  3. If you attempt to delete the currently chosen branch, you will receive an error.

Also Read:

How to undo the most recent commits in Git