简体   繁体   English

SQUASHED时检查Git分支是否已合并为主分支?

[英]Checking if a Git branch has been merged into master when SQUASHED?

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. 最初,我的计划是使用git merge-base来查看最后一次常见提交。

However, it turns out that we squash all of our branches as we merge them into master. 然而,事实证明,当我们将它们合并为master时,我们压缩所有分支。 As this creates a new commit hash, I'm unable to find the common commits between master and my branches. 由于这会创建一个新的提交哈希,我无法找到master和我的分支之间的常见提交。

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: 您可以进行实验性合并,并检查是否引入了任何更改,如下所示:

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 changed in the original git merge --squash foo have seen further conflicting changes on master since then. 这只有在原始git merge --squash foo没有更改文件的情况下才会起作用git merge --squash foo从那时起就在master上看到了进一步的冲突变化。

Here's one approach: 这是一种方法:

  1. Use the GitHub API to get the HEAD SHA for all merged pull requests. 使用GitHub API获取所有合并拉取请求的HEAD SHA。
  2. Match these SHAs against local branches. 将这些SHA与本地分支进行匹配。

If a local branch's HEAD has corresponds to that of a PR that's been merged, then you can safely delete it. 如果本地分支的HEAD对应于已合并的PR的HEAD,则可以安全地删除它。 This will work regardless of how the PR was merged (full merge, fast foward, squash and merge). 无论PR如何合并(完全合并,快速前进,压缩和合并),这都将有效。

I implemented this approach in a delete-squashed-branches script. 我在delete-squashed-branches脚本中实现了这种方法。 Here's what usage looks like: 这是用法的样子:

(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

(The warnings can be ignored since the branches were merged onto master via Squash & Merge.) (警告可以忽略,因为分支通过Squash&Merge合并到master上。)

Caveats: 注意事项:

  • You'll need to pip install pygithub to use the script. 您需要使用pip install pygithub来使用该脚本。
  • It will look for local branches that were merged onto origin/(current branch) . 它将查找合并到origin/(current branch)本地分支。 So you'll want to run it on master , develop or whatever branch you work off of. 所以你想要在masterdevelop或你工作的任何分支上运行它。
  • If you have multiple long-lived branches, this approach won't work very well. 如果你有多个长寿分支,这种方法将无法很好地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 合并到 master 时,第一个分支被压扁后合并分支的一个分支 - Merging a branch of a branch after first branch is squashed when merged to master 分支合并到 master 后的 git rebase - git rebase after branch has been merged into master 知道第三个分支何时合并到 master - Know when a third branch has been merged to master 在子分支已合并回master之后,VSTS Git将孙分支合并到master - VSTS Git merge grandchild branch into master after child branch has been merged back into master 如何将远程主服务器重置回尚未与git中的另一个分支合并的提交 - How to reset remote master back to a commit which has not been merged with another branch in git 当我压缩来自父分支的提交并将其合并到主分支时,子分支会发生什么? - What happens to the child branch when I have squashed the commits from parent branch and merged it into master? Git:如何删除已经提交,推送并合并到master的分支? - Git: How do I remove a branch that has already been committed, pushed, and merged into master? 我可以安全地删除已合并到master的Git主题分支吗? - Can I safely remove my Git topic branch that has been merged into master? 在GitHub中合并+压缩PR到master后,如何同步你的git dev分支? - How to sync up your git dev branch after you've merged+squashed a PR to master, in GitHub? 检查develop分支是否已合并到功能分支中? - Checking if develop branch has been merged into feature branch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM