DEV Community

Prashant Sharma
Prashant Sharma

Posted on

How to delete a git branch (local and remote)

To delete a Git branch both locally and remotely, follow these steps:

1. Delete a Local Branch

Run the following command to delete a local branch:

git branch -d <branch_name>
Enter fullscreen mode Exit fullscreen mode
  • Use -d for a safe deletion, which will only delete the branch if it has been fully merged.
  • Use -D if you want to force delete the branch, even if it hasn’t been merged.

Example:

git branch -d my-feature-branch
Enter fullscreen mode Exit fullscreen mode

If the branch hasn't been merged, force delete it with:

git branch -D my-feature-branch
Enter fullscreen mode Exit fullscreen mode

2. Delete a Remote Branch

Run the following command to delete a branch from the remote repository:

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

Example:

git push origin --delete my-feature-branch
Enter fullscreen mode Exit fullscreen mode

This will remove the branch from the remote (e.g., GitHub, GitLab).

Top comments (2)

Collapse
 
monica_exists profile image
Monica Blake

Simple. Nice. πŸ‘Œ

Collapse
 
shadow-07 profile image
Sohel

Nice one