Source Controlgit

Delete Git branch locally and remotely

I stumble across this one every so often. And since I have to look it up every time, I may as well document it. Today's post is small and easy.

To delete a Git branch after the completion of a feature or bug fix, you can use the following commands. Cause the CLI rules, right, RIGHT?

Anyway, back to the task at hand.

## Delete a remote branch
$ git push origin --delete <branch> # Git version 1.7.0 or newer 
$ git push origin :<branch> # Git versions older than 1.7.0

## Delete a local branch
$ git branch --delete <branch>
$ git branch -d <branch> # Shorter version
$ git branch -D <branch> # Force delete un-merged branches

## Delete a local remote-tracking branch
$ git branch --delete --remotes <remote>/<branch>
$ git branch -dr <remote>/<branch> # Shorter
$ git fetch <remote> --prune # Delete multiple obsolete tracking branches
$ git fetch <remote> -p # Shorter

View GitDeleteCommands.ps1 on GitHub

As you realize, there are 3 different branches that need to be deleted in Git:

  1. The local <branch>
  2. The remote origin/<branch>.
  3. The local remote-tracking branch origin/<branch> that tracks the remote <branch>

No more googlebinging for this. Now you know where to get the answer :)

RELATED POSTS