About this question
To delete a Git branch:
Locally:
git branch -d branch-name # Use -D to force delete
Remotely:
git push origin --delete branch-name
To delete a Git branch:
Locally:
git branch -d branch-name # Use -D to force delete
Remotely:
git push origin --delete branch-name
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask DevOps Expert
Answered on Mar 18, 2025
When working with Git, you may need to delete branches that are no longer needed. You can delete a branch both locally and remotely using simple commands.
1. Deleting a Local Branch
git branch -d branch-nameThe -d flag deletes the branch but prevents deletion if it has unmerged changes.
If the branch is not fully merged, use:
git branch -D branch-nameThis force deletes the branch, even if there are unmerged changes.
2. Deleting a Remote Branch
git push origin --delete branch-name3. Removing Deleted Remote Branch Locally
git fetch --pruneCleans up deleted remote branches from your local Git references.
Important Notes:
You cannot delete the branch you’re currently on, so switch to another branch first:
git checkout mainBe careful when using -D as it permanently removes unmerged work.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common DevOps interview questions, answered
Guides, tips and career advice on DevOps from JanBask experts.
DevOps How to Become a DevOps Engineers: Learning Path, Critical skills & More
Looking for answer to how to become a DevOps engineer in 2025? Here is the detailed article about DevOps career path, DevOps…
DevOps Top 50+ Jenkins Interview Questions And Answers For 2025
Here are the Top 50 Jenkins interview questions frequently asked in top MNCs which will help you land an ideal job! These…
DevOps Best 37 DevOps Tools To Learn And Master In 2025: Docker, Git & More
Want to know what are Devops tools and their uses? Or looking for the best DevOps list for automation, monitoring, & development?…
DevOps DevOps Engineer Job Description: Roles and Responsibilities [2025 Updated]
Discover the essential roles and responsibilities of a DevOps Engineer by exploring the detailed DevOps job description, and…