I'd like to automate cleaning of remote branches. I want to be able to check to see if a branch has been merged into master already. Originally, my plan was to use git merge-base to see the last common commit.
However, it turns out that we squash all of our branches as we merge them into master. As this creates a new commit hash, I'm unable to find the common commits between master and my branches.
How might I go about determining if a branch has been merged if we've squashed them all when merging?
Thanks in advance!
You could do an experimental merge and check whether it introduces any changes, like so:
This will only work if none of the files changed in the original
git merge --squash foo
have seen further conflicting changes onmaster
since then.Here's one approach:
If a local branch's HEAD has corresponds to that of a PR that's been merged, then you can safely delete it. This will work regardless of how the PR was merged (full merge, fast foward, squash and merge).
I implemented this approach in a delete-squashed-branches script. Here's what usage looks like:
(The warnings can be ignored since the branches were merged onto master via Squash & Merge.)
Caveats:
pip install pygithub
to use the script.origin/(current branch)
. So you'll want to run it onmaster
,develop
or whatever branch you work off of.