How to delete a git branch (local and remote)

sharmaprash

Prashant Sharma

Posted on September 7, 2024

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).

💖 💪 🙅 🚩
sharmaprash
Prashant Sharma

Posted on September 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related