Tutorials  /  Linux Basics

How to Delete Git Branches Locally and Remotely

Ccentron Redaktion · September 2025 ·6 min read ·Linux Basics, Tutorial

Git branches act as independent environments for development. They allow you to work on new features, fixes, or experiments without impacting the main project. Each branch keeps its own commit history, making it possible to test safely before merging. Over time, unused or already merged branches may clutter your repository and complicate collaboration. Regular branch cleanup, whether after merging, finishing tasks, or discarding experiments, keeps your project organized and manageable. This guide explains Git branch handling and demonstrates how to remove them locally and remotely.

The Short Answer Version

If you already know Git and need a quick reference for deleting branches, use the following commands:

Console
# Delete a local branch (safe, won’t remove unmerged changes)
$ git branch -d <branch_name>

# Force delete a local branch (removes even if unmerged)
$ git branch -D <branch_name>

# Delete a remote branch
$ git push origin --delete <branch_name>

Keep reading for detailed explanations and examples of each case.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Delete a Branch in Git Locally

Once a feature branch has been merged into the main branch, you can safely remove it from your local repository. Git includes built-in protection to avoid accidental deletion.

Command Explanation (Local)

Use the git branch command with either -d or -D. The lowercase flag only deletes if merged, while the uppercase version forces deletion even when unmerged.

Command Syntax (Local)

To delete a branch locally:

Console
git branch -d <branch_name>
git branch -D <branch_name>
  • git branch: Handles branch operations.
  • -d: Deletes only if already merged.
  • -D: Forcefully deletes regardless of merge state.
  • <branch_name>: Name of the branch to delete.

Command Flags Overview (Local)

Additional flags for managing branches:

Flag Description
-d, --delete Deletes only if merged
-D Force deletes regardless of merge
-v, --verbose Shows last commit for each branch
-a, --all Lists local and remote branches
--merged Lists branches merged into current branch
--no-merged Lists branches not yet merged

Command Demonstration (Local)

Example workflow for deleting local branches:

List all local branches:

Console
$ git branch
  development
  feature-login
  hotfix-header
* main

Check merged branches:

Console
$ git branch --merged
  development
  feature-login
* main

Delete a merged branch:

Console
$ git branch -d feature-login
Deleted branch feature-login (was 8cd77fa).

Attempt to delete an unmerged branch:

Console
$ git branch -d hotfix-header
error: the branch 'hotfix-header' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D hotfix-header'

Force-delete the unmerged branch:

Console
$ git branch -D hotfix-header
Deleted branch hotfix-header (was a0716a9).

Recheck remaining branches:

Console
$ git branch
  development
* main

Deleting a branch only removes its reference. If unmerged, its commits may become unreachable and eventually cleaned by Git’s garbage collector.

Delete a Branch in Git Remotely

After deleting a branch locally, you may also remove it on the remote repository. This keeps the project consistent across all contributors and prevents unnecessary clutter.

Command Overview (Remote)

To remove a remote branch, use git push with --delete. Unlike local deletion, Git does not check merge status for remote deletions.

Command Syntax (Remote)

Modern syntax:

Console
git push <remote> --delete <branch_name>
  • git push: Updates the remote repository.
  • : Typically “origin”.
  • --delete: Requests removal of the branch.
  • <branch_name>: The branch to delete.

Legacy alternative syntax:

Console
git push <remote> :<branch_name>

The colon before the branch name signals Git to delete it from the remote repository.

Command Flags Overview (Remote)

When deleting branches on a remote repository, Git offers two approaches: the modern --delete flag and the older colon-based method. While remote deletions don’t include safeguards for unmerged branches, both commands achieve the same outcome. Here’s a summary of helpful flags for remote deletions:

Flag Description
--delete, -d Removes the specified branch from the remote repository
--force, -f Forces the push, even if it’s not a fast-forward (not required for delete)
--dry-run, -n Simulates the command without making changes
--verbose, -v Shows detailed output during the operation

Command Demonstration (Remote)

Here’s a step-by-step example to delete a remote branch and clean up local references.

List all remote branches:

Console
$ git branch -r
origin/HEAD -> origin/main
origin/development
origin/feature-login
origin/hotfix-header
origin/main

Remove the feature-login branch from the remote repository:

Console
$ git push origin --delete feature-login
To https://github.com/<username>/<repository>.git
 - [deleted]         feature-login

Prune deleted references from your local clone:

Console
$ git fetch --prune
From https://github.com/<username>/<repository>.git
 - [deleted]         (none)     -> origin/feature-login

The --prune option removes remote-tracking references that no longer exist on the server.

Check again to confirm the branch is gone:

Console
$ git branch -r
origin/HEAD -> origin/main
origin/development
origin/hotfix-header
origin/main

Remove your local copy of the deleted remote branch:

Console
$ git branch -d feature-login

Other contributors with local clones also need to run the prune command to update their references:

Console
$ git fetch --prune

Note: To delete a remote branch, you must have the appropriate permissions. If you encounter errors, verify your access rights or contact the repository administrator.

Conclusion

Now you know how to delete Git branches both locally and remotely. Whether you are cleaning up merged branches or removing outdated ones, commands like git branch -d, git branch -D, and git push origin --delete help keep your repository organized. You also learned when deletion makes sense and how to avoid pitfalls such as accidentally force-deleting unmerged work. For additional details and advanced usage, consult the official Git documentation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren