找到两个 Git 分支首次分歧的点。

huangapple go评论86阅读模式
英文:

find point where two branches in git FIRST diverged

问题

这略微简化了真实故事,但希望足够接近。
假设我最初只有一个分支:develop。然后我创建了一个新分支:release。
此时,release 和 develop 都指向提交 XYZ。
我在 release 分支上做了一些工作,并偶尔将其合并到 develop。
与此同时,我在 develop 分支上做了大量其他工作。
然后有一天,我醒来想知道我的分支何时首次分叉?
也就是说,我想找到提交 XYZ 的提交哈希。显然的诱惑是使用 "git merge-base",但这只会找到我上次将 release 合并到 develop 时的点,这远晚于提交 XYZ。

英文:

This is slightly simplified from the real story, but hopefully close enough.
Suppose I initially have one branch: develop. Then I create a new branch: release.
At this point, release and develop both point to commit XYZ.
I do some work on the release branch, and occasionally merge it into develop.
Meanwhile, I do lots of other work on the develop branch.
Then one day I wake up and wonder when did my branches first diverge?
That is, I want to find the commit hash of commit XYZ. The obvious temptation is to use "git merge-base" but this just finds the point when I last merged release into develop, which is WAY later than commit XYZ.

答案1

得分: 3

diff -u <(git log --reverse --oneline develop) <(git log --reverse --oneline release) \
   | sed '1,3d; /^[+-]/,$d' | tail -1 | awk '{print $1}'

编辑:sed参数是两个删除(d)命令,用分号分隔。第一个删除是删除第1到3行(包括)。第二个删除是删除从第一行开始以加号或减号开头的行,直到最后一行($)。

英文:

Since both branches have the same root, I think it should be possible to generate a list of commits back to the root commit for each branch, and then compare those two lists reversed until there is a difference. Then the common ancestor commit is the last one before the first difference.

The following command should then print the commit id of the common ancestor commit:

diff -u <(git log --reverse --oneline develop) <(git log --reverse --oneline release) \
   | sed '1,3d; /^[+-]/,$d' | tail -1 | awk '{print $1}'

Edit: The sed argument is two delete (d) commands, separated with semicolon. The first delete is removing lines 1 to 3 (inclusive). The second delete is removing from the first line that starts with either plus or minus, until the last line ($).

huangapple
  • 本文由 发表于 2023年4月11日 05:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定