DEV Community

Ankit Verma
Ankit Verma

Posted on

How to delete a Git branch locally and remotely?

How to delete a Git branch locally and remotely?

Delete a local branch
Safe delete (recommended)
Deletes the branch only if it’s already merged.

git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode

Force delete (even if not merged)
Use this carefully:

git branch -D branch_name
Enter fullscreen mode Exit fullscreen mode

Delete a remote branch

git push origin --delete branch_name
Enter fullscreen mode Exit fullscreen mode

Delete local + remote together (common workflow)

git branch -d branch_name
git push origin --delete branch_name
Enter fullscreen mode Exit fullscreen mode

Important

  • You cannot delete the branch you are currently on
git checkout main
Enter fullscreen mode Exit fullscreen mode
  • Replace branch_name with your actual branch name
  • origin is the default remote — change it if needed

Verify deletion

List local branches

git branch
Enter fullscreen mode Exit fullscreen mode

List remote branches

git branch -r
Enter fullscreen mode Exit fullscreen mode

Clean up stale remote branches

git fetch -p
Enter fullscreen mode Exit fullscreen mode

Quick Action Command

  1. Delete local (safe): git branch -d branch
  2. Delete local (force): git branch -D branch
  3. Delete remote: git push origin --delete branch
  4. Cleanup: git fetch -p

Top comments (0)