How to Delete a Local Git Branch

I’m posting this here as a reference for myself, but know that this may help others in a similar situation.

I don’t often delete branches in Git, but when I do I like to make sure that I am doing so correctly.

The below guide to How to Delete a Git Branch is directly from the highest rated answer on StackOverflow.

If one wishes to delete a branch named “Test_Branch.”

  1. Switch to some other branch to avoid any errors when attemtping to delete. For example, I will switch to the master.
$ git checkout master
  1. Use the ‘branch -d’ command followed by your branch name to delete. The -d flag here specifies the delete operation.
$ git branch -d Test_Branch
  1. If above command gives you error - The branch ‘Test_Branch’ is not fully merged. If you are sure you want to delete it and still you want to delete it, then you can force delete it using -D instead of -d, as:
$ git branch -D Test_Branch
  1. To delete Test_Branch from remote as well, execute:
git push origin --delete Test_Branch