2025-05-11

Cleaning Branches After Squash and Merge

Programming
Notes
This article was translated by GPT-5.2-Codex. The original is here.

Introduction

When you merge branches on GitHub, there are three options.

  • Create a merge commit
  • Rebase and merge
  • Squash and merge

At work I often use Create a merge commit, but in personal projects I sometimes use Squash and merge to avoid keeping small fix commits on master, since the problems that arise are usually less troublesome.

For the common issues with Squash and merge, please refer to articles you can find by searching.

The conflict issue after squash merge can apparently be solved with a rebase option.

However, when deleting merged branches, you must use -D instead of -d. I regularly run git pull --prune to clean up merged local branches, so it is inconvenient that I can't delete them with a single command.

For --no-ff

When using Create a merge commit, I set the following alias in ~/.config/git/config and delete merged branches with git delete-merged-branch.

1
[alias]
2
delete-merged-branch = "!f () { git checkout $1; git branch --merged|egrep -v '\\*|develop|main|master'|xargs git branch -d; git fetch --prune; };f"

I modified this alias based on the following articles to protect develop, main, and master.

But with this, branches merged with Squash and merge are not removed.

Want to support Squash and merge

When I searched, I found four methods.

The first is poi, developed as a gh extension. The second and third are Node packages that can be run with npx. The fourth is a shell-script implementation of git-delete-squashed.

gh-poi

I decided to try poi. First, log in with gh.

1
$ gh auth login
2
? Where do you use GitHub? GitHub.com
3
? What is your preferred protocol for Git operations on this host? SSH
4
? Generate a new SSH key to add to your GitHub account? Yes
5
? Enter a passphrase for your new SSH key (Optional):
6
? Title for your SSH key: GitHub CLI
7
? How would you like to authenticate GitHub CLI? Login with a web browser
8
9
! First copy your one-time code: XXXX-XXXX
10
Press Enter to open https://github.com/login/device in your browser...
11
Authentication complete.
12
- gh config set -h github.com git_protocol ssh
13
Configured git protocol
14
Uploaded the SSH key to your GitHub account: /Users/suzumiyaaoba/.ssh/id_ed25519.pub
15
Logged in as SuzumiyaAoba

If you haven't used the gh command, install it by following Installation.

After logging in, install gh-poi.

1
$ gh extension install seachicken/gh-poi
2
Installed extension seachicken/gh-poi

When I ran gh poi to delete squash-merged branches, I got the following.

1
$ gh poi
2
Fetching pull requests...
3
Deleting branches...
4
5
Deleted branches
6
improve-header
7
└─ #38 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/38 SuzumiyaAoba
8
posts
9
└─ #42 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/42 SuzumiyaAoba
10
11
Branches not deleted
12
clean
13
└─ #29 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/29 SuzumiyaAoba
14
* master
15
pagefind
16
└─ #24 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/24 SuzumiyaAoba

It did delete squash-merged branches. Unlike git-delete-squashed, it seems to determine whether a branch is squash-merged based on GitHub PR information.

Fetching data from GitHub might not be ideal, but I'll try it for a while. If I run into problems, I may also try git-delete-squashed.