DEV Community

Prashant Sharma
Prashant Sharma

Posted on

5

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

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay