我想自动清理远程分支机构。我希望能够检查分支是否已经合并为主分支。最初,我的计划是使用git merge-base来查看最后一次常见提交。
然而,事实证明,当我们将它们合并为master时,我们会压缩所有分支。由于这会创建一个新的提交哈希,我无法找到master和我的分支之间的公共提交。
如果我们在合并时将它们全部压扁,我怎么能确定分支是否已合并?
提前致谢!
答案 0 :(得分:2)
您可以进行实验性合并并检查是否引入了任何更改,如下所示:
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
仅当原始git merge --squash foo
中的所有文件都没有在master
之后发生进一步的冲突更改时,此功能才有效。
答案 1 :(得分:1)
这是一种方法:
如果本地分支的HEAD与已合并的PR的HEAD相对应,则可以安全地将其删除。无论PR如何合并(完全合并,快速前进,压缩和合并),这都将有效。
我在delete-squashed-branches脚本中实现了这种方法。这是什么用法:
(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
(警告可以忽略,因为分支通过Squash& Merge合并到主人身上。)
注意事项:
pip install pygithub
才能使用该脚本。origin/(current branch)
的本地分支。因此,您希望在master
,develop
或您工作的任何分支上运行它。