【问题标题】:How to see if changes from a branch are already done in another one?如何查看一个分支的更改是否已经在另一个分支中完成?
【发布时间】:2018-12-17 14:54:19
【问题描述】:

我想知道我在 BranchA 上的所有代码更改是否已在 BranchB 上应用,无论提交 ID 是否相同或 BranchB 是否有大量额外更改

这可能吗?

例子:

 Branch A :  Commit 111... -> Add line 23
             Commit 222... -> Add line 24
 Branch B:   Commit 333... -> Add line 23
             Commit 444... -> Add line 24
             Commit 555... -> Remove line 24
             Commit 666... -> Add line 25
             Commit 777... -> Add line 26
What I basically would like to know is whether the two changes from Branch A are already applied by other commits on Branch B.
In this case, the output I would like to see is:

  - Add line 24

Why? Because this is the only change from Branch A not matching latest state of Branch B. I want to discard extra staff exclusive in Branch B (commits 666 and 777). I also want to consider latest state of Branch B, so even Branch B added at some point line 24, the latest state is removed, so that is the only differing change in regards to Branch A

当使用比较和差异时,我通常会看到分支 B 的所有额外员工,我不感兴趣,因为我只想知道分支 A 上下文中的差异

【问题讨论】:

    标签: git branch git-log


    【解决方案1】:

    你也可以这样做:

    git branch --contains BranchA
    

    列出具有所有 BranchA 提交的所有分支。如果您在列表中找到了您的 BranchB,那就很好。

    如果有很多分支,请使用管道 grep :

    git branch --contains BranchA | grep BranchB
    

    在 cmets 之后编辑:

    如果您必须检测更改而不管它们属于哪个提交,例如当您重新定位或挑选一些工作时,如kostix suggests,您可以这样做:

    git log --cherry BranchB..BranchA
    

    但话又说回来,虽然 检测 更改将发生在代码级别,但无论提交哈希如何,输出本身将默认作为提交给出。要查看这些提交中的实际代码更改,您可以使用 -p 选项,并且可能将其存储在文件中:

    git log -p --cherry BranchB..BranchA > output.txt
    

    【讨论】:

    • 但这是在提交级别。我喜欢在代码级别做。这可能吗?
    • @Whimusical,是的,见git log --cherry
    • 在我的本地尝试中,输出仍然显示提交并且没有代码行。
    • @RomainValeri 我在描述中放了一个更清晰的例子
    • @Whimusical 你说得对,我忽略了输出类型/格式,我已经编辑了答案。希望它适合你想要的。
    【解决方案2】:

    如果您合并所有更改,这可以通过 git log BranchB..BranchA 完成(即“给我所有在 A 而不是 B 中的提交”)。如果这不返回任何内容,则应用所有 A 更改。

    如果您使用变基或以完全不同的方式实施更改,则无法检查这一点,因为您的更改可能在以后被更改或完全覆盖。您可以尝试 grep 一些相关行或构建软件,看看它是否具有您想要的功能,但没有通用的方法。

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      相关资源
      最近更新 更多