Notes
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.
- I stopped using squash merge in Git - Mobile Factory Tech Blog
- Graduating from squash merge and moving to a branch strategy that accelerates product development | SocialDog Tech Blog
The conflict issue after squash merge can apparently be solved with a rebase option.
- Resolve conflicts for branches containing squash-merged commits in Git #onto - Qiita
- When Cascading PRs conflict in a squash merge environment - oinume journal
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]2delete-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.
- seachicken/gh-poi: ✨ Safely clean up your local branches
- not-an-aardvark/git-delete-squashed: Delete branches that have been squashed and merged into main
- teppeis/git-delete-squashed: Delete branches that have been squashed and merged into master
- Delete branches squashed on GitHub · ryym.log
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 login2? Where do you use GitHub? GitHub.com3? What is your preferred protocol for Git operations on this host? SSH4? Generate a new SSH key to add to your GitHub account? Yes5? Enter a passphrase for your new SSH key (Optional):6? Title for your SSH key: GitHub CLI7? How would you like to authenticate GitHub CLI? Login with a web browser89! First copy your one-time code: XXXX-XXXX10Press Enter to open https://github.com/login/device in your browser...11✓ Authentication complete.12- gh config set -h github.com git_protocol ssh13✓ Configured git protocol14✓ Uploaded the SSH key to your GitHub account: /Users/suzumiyaaoba/.ssh/id_ed25519.pub15✓ 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-poi2✓ Installed extension seachicken/gh-poi
When I ran gh poi to delete squash-merged branches, I got the following.
1$ gh poi2✔ Fetching pull requests...3✔ Deleting branches...45Deleted branches6improve-header7└─ #38 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/38 SuzumiyaAoba8posts9└─ #42 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/42 SuzumiyaAoba1011Branches not deleted12clean13└─ #29 https://github.com/SuzumiyaAoba/SuzumiyaAoba.github.io/pull/29 SuzumiyaAoba14* master15pagefind16└─ #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.