V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
RedBeanIce
V2EX  ›  git

[git]如何查看远程分支的来源自哪个分支

  •  
  •   RedBeanIce · 2022-04-14 14:59:29 +08:00 · 2697 次点击
    这是一个创建于 743 天前的主题,其中的信息可能已经有所发展或是发生改变。
    git reflog show origin/offer_branch1
    
    git reflog | grep offer_branch1
    
    

    网上的方法都实验了一下,没有用

    我的目标是,我远程有一个分支,我想知道他是从哪里来的(从 mater 呢,还是从哪里)

    D:\git\offer>git reflog show origin/offer_branch1
    c771b09ec (origin/offer_branch1, offer_branch1) refs/remotes/origin/offer_branch1@{0}: update by push
    b6c0dc035 refs/remotes/origin/offer_branch1@{1}: update by push
    4dd06da86 refs/remotes/origin/offer_branch1@{2}: update by push
    9ca1762e4 refs/remotes/origin/offer_branch1@{3}: update by push
    cdaf15d33 refs/remotes/origin/offer_branch1@{4}: update by push
    14507a4ae refs/remotes/origin/offer_branch1@{5}: update by push
    0f2911036 refs/remotes/origin/offer_branch1@{6}: update by push
    01fe3bc21 refs/remotes/origin/offer_branch1@{7}: update by push
    8b15c769d refs/remotes/origin/offer_branch1@{8}: update by push
    756392784 refs/remotes/origin/offer_branch1@{9}: update by push
    9537dd380 refs/remotes/origin/offer_branch1@{10}: fetch origin --recurse-submodules=no --progress --prune: storing head
    
    
    $ git reflog | grep offer_branch1
    c771b09ec HEAD@{0}: checkout: moving from offer_branch to offer_branch1
    401ed70fa HEAD@{1}: checkout: moving from offer_branch1 to offer_branch
    c771b09ec HEAD@{2}: checkout: moving from offer_temp3 to offer_branch1
    a5ab1a592 HEAD@{3}: checkout: moving from offer_branch1 to offer_temp3
    c771b09ec HEAD@{4}: checkout: moving from offer_temp2 to offer_branch1
    f94f145bb HEAD@{5}: checkout: moving from offer_branch1 to offer_temp2
    c771b09ec HEAD@{8}: checkout: moving from offer_temp2 to offer_branch1
    f94f145bb HEAD@{9}: checkout: moving from offer_branch1 to offer_temp2
    c771b09ec HEAD@{12}: checkout: moving from offer_temp3 to offer_branch1
    062a48dbd HEAD@{58}: merge origin/offer_branch1: Merge made by the 'recursive' strategy.
    f3b731970 HEAD@{65}: checkout: moving from offer_branch1 to offer_temp
    cdaf15d33 HEAD@{70}: checkout: moving from offer_temp to offer_branch1
    1b967ae32 HEAD@{73}: checkout: moving from offer_branch1 to offer_temp2
    14507a4ae HEAD@{75}: checkout: moving from offer_temp2 to offer_branch1
    f3b731970 HEAD@{78}: checkout: moving from offer_branch1 to offer_temp
    8b15c769d HEAD@{82}: checkout: moving from offer_branch to offer_branch1
    6327e6b40 HEAD@{88}: checkout: moving from offer_branch1 to offer_branch
    756392784 HEAD@{90}: checkout: moving from offer_branch to offer_branch1
    48b39e0be HEAD@{93}: checkout: moving from offer_branch1 to offer_branch
    9537dd380 HEAD@{95}: checkout: moving from offer_branch to offer_branch1
    
    
    15 条回复    2022-04-21 19:39:32 +08:00
    wangsyi13
        1
    wangsyi13  
       2022-04-14 15:07:40 +08:00
    看不到吧,我理解是只能看到从哪个点分出来的,然后能看到这个点都在哪个分支上有,得出来结果不一定是唯一的
    RedBeanIce
        2
    RedBeanIce  
    OP
       2022-04-14 16:42:05 +08:00
    @wangsyi13 也差不多?我就想知道,我的这个分支,是从哪个分支的哪个点拉出来的
    dablwow
        3
    dablwow  
       2022-04-14 18:20:06 +08:00
    是想实现什么目的?看描述有点像是 XY problem
    libook
        4
    libook  
       2022-04-14 18:30:48 +08:00
    如果两个分支从某一次提交前的所有提交都一样,那么可以说这两个分支是同源的。

    但 A 是从 B 分出来还是 B 是从 A 分出来,就没法说了。

    举个例子,有一种操作是从 master 里分出分支 A ,然后没有任何提交又从分支 A 分出分支 B ,此时分支 B 算是从哪个分支分出来的?
    在从 master 分出 A 之后,master 上有新提交,A 上没有,把 master merge 到 A 上,再从 A 分出新分支 B ,此时分支 B 算是从哪个分支分出来的?

    这应该是个 XY problem ,可以说一下背景和需求。
    tairan2006
        5
    tairan2006  
       2022-04-14 18:35:56 +08:00
    用图形化工具看所有分支的 history ,可以看到是从哪里分出来的
    kxuanobj
        6
    kxuanobj  
       2022-04-14 18:56:00 +08:00
    git merge-base 是你想找的吗?或者它的 --fork-point 参数?
    kxuanobj
        7
    kxuanobj  
       2022-04-14 18:56:56 +08:00
    mingyuewandao
        8
    mingyuewandao  
       2022-04-15 10:27:27 +08:00
    git branch -vv
    RedBeanIce
        9
    RedBeanIce  
    OP
       2022-04-15 13:28:59 +08:00 via iPhone
    @dablwow 我已经描述了,远程分支一共 80 个,其中有一个分支是我的,我想知道他是从哪个分支分出去的。这个分支目前都已经有多次提交( 20 来次),,但是由于忘记了或者是什么,我不知道他是从哪个分支分出去的(但是我记得是三个其中的一个,这三个分支目前都已经有多次提交)
    RedBeanIce
        10
    RedBeanIce  
    OP
       2022-04-15 13:30:18 +08:00 via iPhone
    @libook 请看最新,增加了一些描述,但是应该不是 xy

    @tairan2006 请问可以推荐吗,可能 idea 的树形图没看懂
    RedBeanIce
        11
    RedBeanIce  
    OP
       2022-04-15 13:34:57 +08:00 via iPhone
    @kxuanobj 谢谢,我看了一下,但是好像是可以找到两个分支相同点的最后一次提交吗,,,假如我有一个分支 master ,然后有一个迭代分支,有三个人开发从迭代分支拉出去了三个,那么我在三个迭代分支任意一个用 merge-base ,master 或者是迭代主分支,那么应该都是最后一次提交,,那么我如何知道这个人是从迭代主分支拉的还是从 master 拉的呢
    RedBeanIce
        12
    RedBeanIce  
    OP
       2022-04-15 13:35:40 +08:00 via iPhone
    @mingyuewandao 您的回答是不对的,您这里展示的是本地分支以及远程分支的对应关系吧,我的描述中都是写的远程分支
    wolfie
        13
    wolfie  
       2022-04-15 14:36:38 +08:00   ❤️ 1
    git log --no-walk --decorate
    看起来像是该分支的 首次 commit ,merge 了其他分支后不清楚会不会生效。


    https://stackoverflow.com/a/61258386
    https://devops.stackexchange.com/questions/3321/what-git-command-could-be-used-to-check-when-a-remote-branch-was-created
    SoloCompany
        14
    SoloCompany  
       2022-04-15 19:03:30 +08:00   ❤️ 1
    简单回答,不能

    branch A: a -> b -> c
    branch B: a -> b -> d
    branch C: a -> b -> e

    请问 C 是从 A 还是 B 分支出来的?

    这是一个伪问题

    正确的问题应该是, 找出 C 和一堆分支(比如 A / B) 的基点 (就是上面已经有人回答过的 merge-base)

    曾经写过一个很简单的脚本完成这个任务

    git merge-base HEAD `git for-each-ref refs/remotes/origin/master refs/tags | cut -c1-40`

    可以找到当前分支和 master 以及所有 tags 的最近的交点, 至于这个交点应该判定给 master 还是某个 tags 那只能自己判断

    当然可以基于这个脚本改进一下, for-each-tags 执行 git describe base-commit, 数字最小的就是可能的答案
    kxuanobj
        15
    kxuanobj  
       2022-04-21 19:39:32 +08:00
    @RedBeanIce 因为 master 和迭代主分支可能共享同一个 commit 。这种时候从哪个分支拉都是一样的结果。

    你可以认为分支是一个特殊的 TAG (实际上分支和 tag 在 git 底层是 reference ),他们只是标识一个 commit 。注意,这里是一个。

    当你在 master 分支做一次提交后,git 会根据当前 master 这个 ref 找到 commit id ,将新 commit 的 parent 设置为这个 commit 。然后移动 master 这个 reference 到新 commit 。
    从 git 角度来讲,分支只是标识一个 commit 而已,并不记录其它信息。真正有前后关联关系的是 commit 自己。
    你甚至可以直接用 git reset --hard 去强行移动一个分支到任意一个 commit 。

    所以如果你的仓库有这些 commit:
    ```
    a->b->c
    \ \->d
    \-e
    ```
    假设 master 分支指向 a ,迭代主分支指向 b ,开发分支分别指向 c 、d 、e 。此时,你想知道”c 是从 a 拉的,还是从 b 拉的“,这个问题是无解的。
    因为开发分支 c 可以是从 a 拉的,但提交了 b 、c 两个 commit 。也可以是 b 这个 commit 已经存在,然后开发分支 c 只提交了 c 这个 commit 。
    这两种情况在 git 记录里是一模一样的,无法区分的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   962 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:38 · PVG 04:38 · LAX 13:38 · JFK 16:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.