Deleting branches in Git keeps your repository clean and organized. There are three main types of branches in Git: local, remote, and local remote-tracking branches. Understanding the differences between these branches is crucial for effective branch management. This guide will walk you through how to delete each type of branch and how to verify that a branch was successfully deleted.
Branch deletion commands
Below are the core Git commands for deleting different types of branches:
How to delete a local branch:
Local branches are branches that exist in your local repository stored directly on your machine. Local branches are where you make your changes before pushing them to the remote branch. Replace <branch-name>
with the name of the branch you want to delete.
git branch --delete <branch_name>git branch -d <branch_name> #short
The -d
option deletes the branch only if it has been merged. If you want to force delete an unmerged branch, use the -D
option instead:
git branch -D <branch_name>
To verify that a local branch was deleted, list the available branches:
git branch
The deleted branch should not appear in the list.
How to delete a remote branch
Remote branches are the branches that live in the remote repository on your VCS. Remote branches track the history and changes of the branch over time while ensuring data redundancy.
To delete a remote branch use these commands:
For Git versions 1.7.0 or newer
git push origin --delete <branch_name>git push origin -d <branch_name> # short
For Git versions older than 1.7.0
git push origin :<branch_name>
To verify that a remote branch was deleted, list the remote branches:
git branch -r
The deleted branch should not appear in this list.
How to delete a local remote-tracking branch:
Local remote-tracking branches are local references to the state of remote branches. They help you track how your local branches diverge from the branches on the remote repository.
Deleting a Local Remote-Tracking Branch: Generally, local remote-tracking branches are deleted automatically when the corresponding remote branch is deleted. However, in the case you deleted the remote branch outside of the CLI, directly on the GitHub web app for example, you’ll end up with an outdated remote-tracking branch on your local machine.
There’s a few main ways to delete these obsolete branches.
To delete the remote tracking branch directly:
git branch --delete --remotes <remote>/<branch_name>git branch -dr <remote>/<branch_name>
As of Git version 1.6.6, you can also run git fetch
with the --prune
flag.
From to the 1.6.6 release notes:
"git fetch" learned --all and --multiple options, to run fetch from many repositories, and --prune option to remove remote tracking branches that went stale. These make "git remote update" and "git remote prune" less necessary
git fetch <remote> --prunegit fetch <remote> -p
One important thing to note is that this will remove all local remote-tracking branches that don’t have a corresponding remote branch upstream.
A third way is to use the remote
command.
To get a list of all of the stale local branches run:
git remote prune origin --dry-run
If you run the command without the --dry-run
flag these branches will all be deleted!
To verify your deletions were successful simply run the dry run command again or use git remote show origin
to see information about every remote and local branch in your repo.
Local branch vs. remote origin branch vs. local remote-tracking
To summarize:
Local branch deletion:
- Deletes the local copy of the branch on your machine.
Remote origin branch deletion:
- Deletes the remote branch stored in your upstream repository
Local Remote-Tracking Branch Deletion:
- Deletes the local tracking information for a corresponding remote branch.
By following these guidelines and understanding the differences between branch types, you can efficiently manage and delete branches in your Git repository, keeping your repository organized and tidy.