Reflect on your 2024 year in code

Deleting branches in git

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

Below are the core Git (and Graphite) commands for deleting different types of branches:

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.

Terminal
git branch --delete <branch_name>
git branch -d <branch_name> # short form

To force delete an unmerged branch, use:

Terminal
git branch -D <branch_name>

Graphite CLI simplifies this process. Use the gt delete command:

Terminal
gt delete <branch_name>

Run the following command to list available branches and confirm deletion:

Terminal
git branch

The deleted branch should no longer appear in the list.

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

Terminal
git push origin --delete <branch_name>
git push origin -d <branch_name> # short

For Git versions older than 1.7.0

Terminal
git push origin :<branch_name>

To verify that a remote branch was deleted, list the remote branches:

Terminal
git branch -r

The deleted branch should not appear in this list.

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.

To delete a remote-tracking branch directly:

Terminal
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:

Terminal
git fetch <remote> --prune
git fetch <remote> -p # shorthand

Graphite CLI automatically removes outdated branches during gt sync, keeping your branches clean.

Run this command to check for outdated tracking branches:

Terminal
git remote prune origin --dry-run

Graphite CLI provides an intuitive way to handle Git branches efficiently. The gt delete command is a powerful tool for managing local branch cleanup:

Terminal
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:

Terminal
gt sync

This not only pulls the latest changes but also prompts you to delete any locally merged branches.

  • Local branch deletion:

    • Deletes the local copy of the branch on your machine.
    • Use git branch -d or gt 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 let gt 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.

Built for the world's fastest engineering teams, now available for everyone