Git Branch --merged vs --no-merged: Essential Guide to Safe Branch Cleanup
Use git branch --merged to identify branches whose commits are already incorporated into the current HEAD (safe to delete), while git branch --no-merged reveals branches containing work that would be lost if deleted.
The tiimgreen/github-cheat-sheet repository documents these critical Git flags that distinguish between disposable history and active development. Understanding the difference between git branch --merged and git branch --no-merged enables automated cleanup workflows that prevent accidental loss of unmerged commits while keeping your repository tidy.
Understanding git branch --merged
According to the source code in README.md at lines 10113-10118, the --merged flag lists all local branches whose tips are reachable from the current HEAD. These branches contain no unique commits—their changes have already been incorporated into your current branch history.
This makes them safe candidates for deletion without risking data loss. When you run git branch -d on a merged branch, Git simply removes the pointer without garbage collecting the underlying commits, since they remain accessible through the current branch's history.
Understanding git branch --no-merged
As documented in README.md at lines 10121-10126, the --no-merged flag displays branches containing commits that are not present in the current HEAD. These branches represent active or abandoned work that has not been integrated.
Deleting branches listed by --no-merged will orphan their unique commits, making them inaccessible through normal branch references. Always review these branches manually before deciding whether to merge, rebase, or force-delete them.
Automated Cleanup Implementation
Listing Merged Branches
To view all branches ready for deletion while excluding the current branch (marked with an asterisk), filter the output with grep:
# Show merged branches excluding the current one
git branch --merged | grep -v '^\*'
The git cleanup Alias
The github-cheat-sheet repository provides a convenient alias defined at README.md lines 998-1000 that automates the deletion of merged branches. Configure it globally with:
git config --global alias.cleanup "!git branch --merged | grep -v '*' | xargs git branch -d"
Execute the cleanup routine by running:
git cleanup
This pipeline lists merged branches, removes the current branch from the list, and passes the remaining names to git branch -d for safe deletion.
Safeguarding Unmerged Work
Before performing bulk deletions, verify which branches still contain unique work:
git branch --no-merged
To inspect specific unmerged branches before integrating or deleting them, use the revision range syntax to see commits unique to that branch:
git log --oneline --graph --decorate origin/master..feature/foo
Summary
git branch --mergedidentifies branches whose tips are already incorporated into the current HEAD, making them safe to delete without losing commit history.git branch --no-mergedlists branches containing commits not present in the current branch, protecting against accidental deletion of unmerged work.- The tiimgreen/github-cheat-sheet repository provides the
git cleanupalias (defined atREADME.md:998-1000) to automate merged branch deletion. - Always run
--no-mergedbefore cleanup operations to review any branches that might contain important unmerged changes.
Frequently Asked Questions
How do I delete all merged branches except the current one?
Pipe the output of git branch --merged through grep -v '^\*' to filter out the asterisk-marked current branch, then use xargs to delete the remaining branches:
git branch --merged | grep -v '^\*' | xargs -n 1 git branch -d
Alternatively, use the git cleanup alias from the github-cheat-sheet repository, which performs this operation in a single command.
Will deleting a branch shown by --no-merged erase my code permanently?
Deleting an unmerged branch removes the branch pointer, but the commits themselves persist in the repository's object database until garbage collection runs. You can recover them using git reflog or by knowing the commit hash. However, without the branch name, the commits become difficult to locate, so treat --no-merged branches as containing work you should review before discarding.
Can I use these flags with remote tracking branches?
Yes, both flags work with remote-tracking branches when prefixed with the remote name. For example, git branch -r --merged lists remote branches already merged into HEAD, while git branch -r --no-merged shows remote branches with unmerged commits. This helps identify stale remote branches before pruning them with git remote prune origin.
Where does the git cleanup alias originate?
The alias is documented in the tiimgreen/github-cheat-sheet repository at README.md lines 998-1000. It defines cleanup as a shell command that chains git branch --merged, grep, and xargs git branch -d to provide a one-command solution for removing merged local branches.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →