Delete a branch locally with git branch -d

To remove a branch from your own computer, use the command git branch -d branch-name, replacing "branch-name" with the actual name of the branch you want to delete. Git will refuse to delete a branch that has unpushed changes, which protects you from accidentally losing work.

If you are certain you want to delete a branch even though it has unpushed commits, use git branch -D branch-name (capital D) instead. This forces the deletion without the safety check. Use this only when you are sure the branch contains nothing you need to keep.

After you run the delete command, the branch is gone from your local machine. You can verify this by running git branch to list all remaining branches — the deleted one will no longer appear.

Key Takeaways

  • Use git branch -d branch-name to delete a branch on your computer; Git will stop you if the branch has unpushed work.
  • Use git branch -D branch-name (capital D) to force deletion of a branch with unpushed commits, but only when you are certain you do not need that work.
  • Deleting a local branch does not affect the branch on a shared server — you must push the deletion separately if other people use that branch.
  • After deletion, run git branch to confirm the branch is gone from your machine.

Delete a branch on a shared server with git push

If your branch exists on a shared server (often called "origin" or "remote"), deleting it from your computer does not remove it from the server. Other people can still see and use it. To delete the remote branch, use git push origin --delete branch-name.

This command tells the server to remove the branch. Anyone else working on that project will still have the branch on their own computer until they run git fetch --prune, which cleans up branches that no longer exist on the server.

If you do not have permission to delete branches on the server, you will see an error message. In that case, ask the person who manages the repository to delete it for you, or check whether your team has a policy about who can remove branches.

Delete both the local and remote branch at once

You can combine both deletions into a single workflow: first delete locally with git branch -d branch-name, then delete on the server with git push origin --delete branch-name. This ensures the branch is gone from both places.

Some teams use a different approach: they delete the remote branch first, then run git fetch --prune on their local machine, which automatically removes local branches that no longer exist on the server. This method is cleaner if multiple people are working on the same project, because it keeps everyone in sync.

What happens when you delete a branch

Deleting a branch does not erase the commits that were on it. If you merged the branch into another branch (like "main") before deleting it, all that work is still there. The commits are only deleted if they were never merged and no other branch points to them.

Git keeps deleted commits in a temporary holding area for about 30 days by default. If you delete a branch by mistake and realize it within a few days, you can usually recover it using git reflog to find the commit hash, then create a new branch pointing to that commit. After 30 days, Git cleans up the temporary data and recovery becomes much harder.

Common reasons to delete a branch

Most teams delete a branch after it has been merged into the main codebase. Once the work is part of the main branch, keeping the old branch around creates clutter and confusion about which branches are still active.

You might also delete a branch if you started work on a feature but decided not to finish it, or if you created a branch by mistake. Cleaning up these abandoned branches keeps your repository organized and makes it easier for team members to see which work is actually in progress.

Prevent accidental deletion with branch protection

If you manage a shared repository, you can set up rules that prevent people from deleting important branches like "main" or "production". On platforms like GitHub, GitLab, and Bitbucket, you can mark certain branches as protected, which means deletion requires approval or is blocked entirely.

This is especially useful for teams, because it stops someone from accidentally deleting the branch that holds your live code. You set up protection rules in the repository settings, and they explore to everyone who uses that repository.

Frequently Asked Questions

Can I recover a branch after I delete it?

Yes, if you deleted it recently. Run git reflog to see a history of changes, find the commit hash of the branch you deleted, then run git checkout -b branch-name commit-hash to recreate the branch. This works for about 30 days; after that, Git removes the temporary data and recovery is not possible.

What is the difference between -d and -D?

git branch -d is the safe option — it refuses to delete a branch that has commits not yet merged into another branch. git branch -D forces deletion without checking. Use -D only when you are certain the branch contains nothing you need.

Do I have to delete a branch after merging it?

No, but most teams do. Deleting merged branches keeps the repository clean and makes it easier to see which work is still in progress. Unmerged branches should usually be kept until you decide what to do with that work.

Will deleting a branch affect the code in main?

No. If you merged the branch into main before deleting it, all that code stays in main. Deleting the branch only removes the branch pointer; the commits themselves remain part of main's history.

What does git fetch --prune do?

git fetch --prune updates your local list of remote branches and automatically deletes any local branches that no longer exist on the server. This keeps your local repository in sync with the shared one.