Git Delete Branch (Local & Remote) with Examples and Fixes

Git Delete Branch (Local & Remote) with Examples and Fixes

Delete Git Branch (Quick Answer)

Delete local branch (safe)

Use the following command to safely delete a local Git branch created using git branch:

text
git branch -d branch_name

This command deletes the branch only if it has been fully merged. It helps prevent accidental data loss.

Force delete local branch

If the branch has unmerged changes and you still want to delete it:

text
git branch -D branch_name

This force deletes the branch without checking merge status. Use with caution.

Delete remote branch (GitHub / GitLab)

To delete a branch from a remote repository using git push:

text
git push origin --delete branch_name

This works for both GitHub and GitLab repositories.

Delete both local and remote branch

Recommended workflow when managing branches:

text
git push origin --delete branch_name
git branch -D branch_name

First delete from remote, then remove it locally to keep everything clean.


What Happens When You Delete a Git Branch?

Does deleting a branch remove commits?

No. Deleting a branch does not immediately delete commits.

Commits remain in Git history until they are garbage collected and can be recovered using git reflog. If no references exist, they may eventually be removed.

Local vs remote branch deletion explained

  • Local branch → Deleted only from your system
  • Remote branch → Deleted from shared repository (GitHub/GitLab)

Deleting one does not automatically delete the other.

Can deleted branches be recovered?

Yes, in most cases.

Use git reflog:

text
git reflog

Find the commit hash and recreate the branch:

text
git branch branch_name <commit_hash>

How to Delete Local Git Branch

Delete branch using git branch -d

Safe deletion:

text
git branch -d feature-branch

Git will prevent deletion if the branch contains unmerged changes, which you can review using git diff examples.

Force delete branch using git branch -D

Force deletion:

text
git branch -D feature-branch

Use this when you are sure the changes are not needed.

Delete multiple local branches

To delete multiple branches at once:

text
git branch -d branch1 branch2 branch3

To force delete multiple branches:

text
git branch -D branch1 branch2 branch3

This helps clean up multiple feature branches quickly.


How to Delete Remote Git Branch

Delete remote branch using git push --delete

To delete a branch from a remote repository (GitHub or GitLab):

text
git push origin --delete branch_name

This command removes the branch from the remote repository but does not affect your local branch.

Alternative syntax for older Git versions

Older Git versions use this syntax:

text
git push origin :branch_name

Both commands achieve the same result, but --delete is the modern and recommended approach.

Verify remote branch deletion

To confirm that the branch is deleted from remote, list remote branches using git branch command:

text
git branch -r

Or fetch updated references using git fetch:

text
git fetch --all --prune

How to Delete Branch in GitLab (UI + CLI)

Delete branch from GitLab UI

Steps:

  1. Open your project in GitLab
  2. Go to Repository → Branches
  3. Find the branch
  4. Click Delete next to the branch

Note: You cannot delete protected branches from UI unless protection is removed.

Delete branch using command line

Use the same Git command:

text
git push origin --delete branch_name

This works for GitLab repositories as well.

GitLab protected branch restrictions

If you see an error while deleting:

  • The branch may be protected (e.g., main, master)
  • Only maintainers or admins can delete protected branches
  • You must unprotect the branch in GitLab settings before deletion

Delete Local and Remote Branch Together

Best practice:

text
git push origin --delete branch_name
git branch -D branch_name

Deleting remote first ensures no one else uses the branch.

Clean up remote tracking branches (git fetch --prune)

After deleting remote branches, clean stale references using git fetch:

text
git fetch --prune

This removes outdated remote-tracking branches like origin/branch_name.


Common Errors While Deleting Git Branch

Cannot delete branch checked out error

Error:

text
error: Cannot delete branch 'branch_name' checked out

Fix:

text
git switch main
git branch -d branch_name

You must switch to another branch before deleting using git switch or git checkout.

Branch is not fully merged error

Error:

text
error: The branch 'branch_name' is not fully merged

Fix options:

text
git merge branch_name
git branch -d branch_name
  • Or force delete:
text
git branch -D branch_name

Cannot delete branch used by worktree

Error occurs when branch is used by another worktree (see Git Worktree concepts via checkout/switch).

Fix:

text
git worktree list
git worktree remove <path>

Then delete the branch.

Remote branch not found error

Error:

text
error: unable to delete 'branch_name': remote ref does not exist

Fix:

  • Verify branch name
  • Fetch latest refs:
text
git fetch --all --prune
  • Check remote branches:
text
git branch -r

Fix: Cannot Delete Branch Checked Out

Switch to another branch before deletion

You cannot delete a branch if you are currently on it.

Fix this by switching to another branch:

text
git switch main

Or:

text
git checkout main

After switching, delete the branch:

text
git branch -d branch_name

Verify current branch

Check which branch you are on:

text
git branch

The current branch is marked with *. Make sure it is not the one you want to delete.


Fix: Branch Not Fully Merged Error

Merge branch before deleting

If Git prevents deletion due to unmerged changes:

text
git merge branch_name
git branch -d branch_name

This ensures changes are safely preserved before deletion.

Force delete when safe

If you are sure the changes are not needed:

text
git branch -D branch_name

This force deletes the branch without checking merge status.


Advanced Branch Cleanup

Delete all local branches except main

To delete all branches except main:

text
git branch | grep -v "main" | xargs git branch -D

Replace main with your default branch if different.

Delete merged branches only

Clean up only merged branches safely:

text
git branch --merged | grep -v "\*" | xargs git branch -d

This removes only branches that are already merged.

Clean stale remote branches

Remove deleted remote branch references:

text
git fetch --prune

This keeps your repository clean and up to date.


Best Practices for Git Branch Deletion

Always verify branch before deleting

List branches before deletion:

text
git branch -a

Ensure you are deleting the correct branch.


Frequently Asked Questions

1. How do I delete a branch in Git?

You can delete a local branch using git branch -d branch_name. To delete a remote branch, use git push origin --delete branch_name.

2. How do I force delete a Git branch?

Use git branch -D branch_name to force delete a local branch even if it has unmerged changes.

3. Why can't I delete a Git branch?

You cannot delete a branch if you are currently on it or if it has unmerged changes. Switch to another branch or use force delete with -D.

4. How do I delete a remote branch in GitLab or GitHub?

Use git push origin --delete branch_name to remove the branch from remote repositories like GitHub or GitLab.

5. What is the difference between git branch -d and -D?

-d safely deletes a branch only if it is fully merged, while -D force deletes the branch regardless of merge status.

6. Can I recover a deleted Git branch?

Yes, you can recover a deleted branch using git reflog to find the last commit and recreate the branch using git branch branch_name <commit-hash>.

7. How do I delete both local and remote branches?

First delete the remote branch using git push origin --delete branch_name, then delete the local branch using git branch -D branch_name.

Summary

In this guide, you learned how to delete Git branches locally and remotely using safe and force methods. We covered commands like git branch -d, git branch -D, and git push origin --delete, along with real-world scenarios and error fixes.

You also explored how to resolve common issues such as deleting a checked-out branch, handling unmerged changes, and cleaning up stale branches. Advanced cleanup commands and best practices help maintain a clean and efficient Git workflow.

By following these methods, you can safely manage and delete Git branches in both local and remote repositories like GitHub and GitLab.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.