How to delete a Git branch locally

How to delete local Git branches

To issue the command to delete a local Git branch, follow these steps:

  • Open a Git BASH window or Command Window in the root of your Git repository
  • If necessary, use the git switch or checkout command to move off the branch you wish to delete
  • use the
    git branch --delete
    command to delete the local branch
  • Run the git branch -a command to verify the local Git branch is deleted

Delete local Git branch command

The command to delete a local git branch can take one of two forms:

  • git branch –delete old-branch
  • git branch -d old-branch

The only difference is the fact that the second local branch delete Git command uses an abbreviated syntax. Both commands do the exact same thing.

Remove vs local Git branch deletes

It should be noted that when you delete a local Git branch, the corresponding remote branch in a repository like GitHub or GitLab remains alive and active. Further steps must be taken to delete remote branches.

When you delete a local Git branch, the deletion will not be reflected in remote repos like GitHub or GitLab.

Delete Local Branch

To delete the local branch use one of the following:

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

  • The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  • The -D option is an alias for --delete --force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]
  • As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  • You will receive an error if you try to delete the currently selected branch.

Leave a Reply

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