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. It also includes how to use Graphite CLI for simpler branch deletion.
Branch deletion commands
Below are the core Git (and Graphite) commands for deleting different types of branches:
How to delete a local branch
Local branches exist in your repository stored directly on your machine. These are where you make changes before pushing them to a remote repository. Replace <branch_name>
with the name of the branch you want to delete.
Using Git:
git branch --delete <branch_name>git branch -d <branch_name> # short form
To force delete an unmerged branch, use:
git branch -D <branch_name>
Using Graphite CLI:
Graphite CLI simplifies this process. Use the gt delete
command:
gt delete <branch_name>
Verifying local branch deletion:
Run the following command to list available branches and confirm deletion:
git branch
The deleted branch should no longer 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 track how your local branches diverge from the branches on the remote repository.
Using Git:
To delete a remote-tracking branch directly:
git branch --delete --remotes <remote>/<branch_name>git branch -dr <remote>/<branch_name>
Alternatively, use git fetch
with the --prune
flag to automatically clean up stale tracking branches:
git fetch <remote> --prunegit fetch <remote> -p # shorthand
Using Graphite CLI:
Graphite CLI automatically removes outdated branches during gt sync
, keeping your branches clean.
Verifying remote-tracking branch deletion:
Run this command to check for outdated tracking branches:
git remote prune origin --dry-run
Using Graphite CLI for advanced branch management
Graphite CLI provides an intuitive way to handle Git branches efficiently. The gt delete
command is a powerful tool for managing local branch cleanup:
gt delete <branch_name>
This command removes the branch locally and ensures better integration with stacked pull requests.
To clean up merged branches in one go, run:
gt sync
This not only pulls the latest changes but also prompts you to delete any locally merged branches.
Summary of branch deletion commands
Local branch deletion:
- Deletes the local copy of the branch on your machine.
- Use
git branch -d
orgt delete
.
Remote branch deletion:
- Deletes the branch from the upstream repository.
- Use
git push origin --delete
.
Local remote-tracking branch deletion:
- Deletes the outdated tracking branch locally.
- Use
git branch -dr
or letgt sync
handle it automatically.
By leveraging both Git and Graphite CLI, you can efficiently manage and clean up branches in your repository, ensuring it stays organized and up-to-date.