How do I delete a Git branch locally and remotely?
Categories:
How to Delete a Git Branch Locally and Remotely

Learn the essential Git commands to effectively remove local and remote branches, keeping your repository clean and organized.
Managing Git branches is a fundamental skill for any developer. As your project evolves, you'll create numerous feature branches, bugfix branches, and experimental branches. Once a branch has served its purpose (e.g., its changes have been merged into main or develop), it's good practice to delete it to keep your repository tidy and improve clarity. This article will guide you through the commands to delete Git branches both from your local machine and from the remote repository.
Understanding Git Branch Deletion
Before diving into the commands, it's important to understand what happens when you delete a branch. Deleting a local branch removes the pointer to the latest commit on your machine. Deleting a remote branch removes that pointer from the shared repository. Neither action typically deletes the actual commits, as long as those commits are reachable from other branches (like main). If a branch contains unique commits that are not merged into any other branch, deleting it will make those commits inaccessible and eventually subject to Git's garbage collection.
main or develop) before proceeding. Deleting a branch with unmerged, unique commits can lead to data loss if not handled carefully.Deleting a Local Git Branch
Deleting a local branch is straightforward. Git provides a command specifically for this purpose. You cannot delete the branch you are currently on, so make sure to switch to another branch first (e.g., main or develop).
1. Step 1
First, switch to a different branch (e.g., main): git checkout main
2. Step 2
To delete a local branch, use the git branch -d command followed by the branch name. This command is a 'safe' delete, meaning Git will prevent deletion if the branch has unmerged changes: git branch -d <branch-name>
3. Step 3
If you need to force delete a local branch, even if it has unmerged changes, use the -D flag. Use this with caution: git branch -D <branch-name>
# Switch to a different branch
git checkout main
# Delete a merged local branch (safe)
git branch -d feature/my-feature-branch
# Force delete a local branch (use with caution)
git branch -D experimental/unmerged-branch
Examples of deleting local Git branches.
Deleting a Remote Git Branch
Deleting a remote branch requires pushing a special 'delete' command to the remote repository. This tells the remote server to remove its pointer to that branch. You'll typically do this after the branch has been merged and deleted locally.
git fetch --prune to update your local copy of remote branches and remove any that no longer exist on the remote.1. Step 1
To delete a remote branch, use the git push command with the --delete flag, specifying the remote name (usually origin) and the branch name: git push <remote-name> --delete <branch-name>
2. Step 2
Alternatively, you can use a shorthand notation where you push an empty string to the branch you want to delete: git push <remote-name> :<branch-name>
# Delete a remote branch using --delete flag
git push origin --delete feature/my-feature-branch
# Alternative shorthand for deleting a remote branch
git push origin :feature/my-feature-branch
Examples of deleting remote Git branches from the 'origin' remote.

Flowchart: Process for Deleting Git Branches
Cleaning Up Stale Remote-Tracking Branches
After deleting remote branches, your local repository might still have references to these deleted remote branches (known as remote-tracking branches, e.g., origin/feature/my-feature-branch). You can clean these up using the git remote prune command.
# View remote-tracking branches that no longer exist on the remote
git remote prune --dry-run origin
# Actually remove stale remote-tracking branches
git remote prune origin
Commands to prune stale remote-tracking branches.
Regularly cleaning up your Git branches, both locally and remotely, is a crucial practice for maintaining a healthy and understandable repository. By following these commands, you can ensure that your branch list remains concise and relevant, making navigation and collaboration much easier for everyone involved in the project.