Verify that the Git branch has been merged with the master when SQUASHED?

I would like to automate cleaning up remote branches. I want to check if the branch has already been merged into a master. My original plan was to use git merge-base to see the last general post.

However, it turns out that we crush all our branches when we combine them into masters. Since this creates a new commit hash, I cannot find common commits between the master and my branches.

How can I determine if a branch was merged if we compiled them at merge?

Thanks in advance!

+5
source share
2 answers

You can perform an experimental merge and check if it makes any changes, for example:

git checkout -b foo-merge-test-branch master git merge --squash foo if [ -n "$(git status --porcelain)" ]; then echo "foo has not been merged"; else echo "foo is already merged"; fi git reset --hard && git checkout master && git branch -d foo-merge-test-branch 

This will only work if none of the files modified in the original git merge --squash foo have since seen additional conflicting changes to master .

+2
source

Here is one approach:

  • Use the GitHub API to get the HEAD SHA for all queries with merged pulls.
  • Map these SHAs to local branches.

If the local HEAD branch corresponds to the volume of the combined PR, you can safely delete it. This will work regardless of how the PR has been combined (full merge, quick capture, squash and merge).

I implemented this approach in a delete-compressed branches script. Here what usage is as follows:

 (master) $ git branch delete-merged-prs fixup unmerged-branch * master (master) $ delete-squashed-branches Finding local branches which were merged onto origin/master via GitHub... warning: deleting branch 'delete-merged-prs' that has been merged to 'refs/remotes/origin/delete-merged-prs', but not yet merged to HEAD. Deleted branch delete-merged-prs (was 325a42b). warning: deleting branch 'fixup' that has been merged to 'refs/remotes/origin/fixup', but not yet merged to HEAD. Deleted branch fixup (was e54f54b). (master) $ git branch unmerged-branch * master 

(Warnings can be ignored since the branches were merged with the master through Squash and Merge.)

Cautions:

  • You need pip install pygithub use the script.
  • It will look for local branches that have been merged onto origin/(current branch) . Thus, you will want to run it on master , develop or any other branch you are working from.
  • If you have multiple branches with a long lifetime, this approach will not work very well.
+1
source

Source: https://habr.com/ru/post/1208195/


All Articles