Skip to content

Meet Graphite Agent — your collaborative AI reviewer, built right into your PR page.

Read more

How to delete all old merged Git branches

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.


Table of contents

A common post-merge task in the development workflow is cleaning up the branches that have already been incorporated into the main branch. This housekeeping is essential for maintaining an organized and efficient repository. The Graphite CLI introduces a straightforward way to handle this cleanup with the gt sync command.

Callout: Remember, gt sync is more than just a clean-up command; it’s a harmonizing tool that aligns your local workspace with the remote repository.

  • Automated housekeeping: The gt sync command streamlines the clean-up process by automatically removing local branches that have been merged remotely.

  • Syncing with remote: It ensures your local branches are up-to-date with the remote branches, reducing the chance of merge conflicts and confusion over branch status.

  1. Update your local repository: Before cleaning up, ensure your local repository is in sync with the remote.

    gt repo sync

  2. Run the sync command: Use gt sync to clean up your branches. This command will sync your current branch with the remote, prune remote-tracking branches, and delete branches that have been merged into the current branch.

    gt sync

    By default, gt sync will clean up branches that have been merged into the branch that you're currently on.

Callout: After running gt sync, your local branch list will be pruned, reflecting only the active branches that are still in development or review.

Integrate the gt sync command into your daily workflow to ensure your repository remains clean and manageable. Running it after a series of merges or at the end of a workday can keep your workspace tidy and up-to-date.

The gt sync command from Graphite is a powerful tool that not only updates your branches but also assists in maintaining a neat repository by removing branches that have served their purpose. Adopting this command into your regular workflow can save time and avoid the confusion that comes with a cluttered branch list.

You can delete all merged branches using gt sync from Graphite CLI, which automatically removes local branches that have been merged remotely. Alternatively, you can use git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d to delete merged branches manually.

Yes, it's generally safe to delete merged branches in git. Once a branch has been merged into the main branch, its changes are preserved in the commit history. However, always ensure the branch has been properly merged before deletion to avoid losing any unmerged work.

After merging a branch, you can delete the local branch using git branch -d <branch-name> for branches that have been merged, or git branch -D <branch-name> to force delete. With Graphite CLI, simply run gt sync to automatically clean up all merged local branches.

Transform your Git workflow today! Get started with Graphite CLI and discover how automated branch management can revolutionize your development process.

For further details on gt sync and other Graphite commands, visit the Graphite documentation. Here you'll find a wealth of information to help you maximize your efficiency with Graphite's suite of tools.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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