Git & GitHub Interview Question #28

How do you delete a Git branch?

Remotes, Fetch, Pull, Push, Merge, Conflicts, Rebase & Stash Junior Beginner

Quick Interview Answer

git branch -d deletes a local branch when Git considers the deletion safe, such as when its work has been merged appropriately. The -D option forces local deletion even when Git detects unmerged work.

Detailed Explanation

git branch -d deletes a local branch when Git considers the deletion safe, such as when its work has been merged appropriately. The -D option forces local deletion even when Git detects unmerged work.

For remote branches, deletion is normally requested through git push with --delete. Always verify that important commits are preserved before force-deleting a branch.

Code Example

# Safe local deletion
git branch -d feature/login

# Force local deletion
git branch -D feature/login

# Delete from remote
git push origin --delete feature/login