找到本地git分支的REMOTE父分支

6
如何获取本地 git 当前提交是从哪个远程分支分支出来的?
我假设这是一个 4 步骤的过程,前三步可能如下:
步骤一:获取本地 git 仓库当前分支的名称:
git rev-parse --abbrev-ref HEAD    


第二步:从本地仓库获取当前检出的提交的哈希值:

git rev-parse HEAD          # full hash  


第三步:获取远程 Git 仓库上游跟踪分支的名称:

git rev-parse --abbrev-ref @{upstream}


第四步:获取远程 git 仓库上父级上游跟踪分支的名称:

需要哪些特定代码来完成这个任务?在阅读了 @ChrisJohnsen 对另一篇文章的回答之后,我想解决方案涉及查找从远程存储库中派生的最新提交。然而,链接帖子中的答案中的代码都似乎是为本地存储库设计的。 这个问题不同,因为我正在询问如何从本地存储库子分支中找到远程存储库父分支。

我添加了 bash 标签,因为这些命令在可以使用 bash 脚本的 CentOS 服务器上运行。

2个回答

2
然而,与链接帖子中的答案中的代码似乎都是为本地仓库设计的。您仍然可以应用该代码并检查结果(最近祖先分支)是否为远程分支(在初始git fetch之后),如果不是,请检查该本地父分支的父分支。

2

如果您想查看全部分支结构以获取远程父分支,您可以通过以下任意一条命令打印提交历史作为图形:

gitk --all
git log --oneline --decorate --graph --all

通过脚本仅获取本地分支的远程父分支,可以基于远程分支创建本地临时分支。然后您可以从临时分支中找到本地分支的父分支。在找到临时分支是本地分支的父分支之后,您可以相应地获取远程分支。最后,删除所有临时分支。详细步骤如下:

  1. List all the remote branches by git branch -r (assume the remote branches are origin/master, origin/dev and origin/feature).

  2. Get the tracking/remote branch from the local branch

    The way to get the remote branch of the given local branch as you use the commmand git rev-parse --abbrev-ref @{upstream} (assume it’s origin/master branch which you want to find it’s parent branch).

  3. Create local temp branches from remote branches separately

    git branch -b origin-master origin/master        #Create a local branch origin-master from origin/master
    git branch -b origin-dev origin/dev
    git branach -b origin-feature origin/feature
    
  4. Find the parent branch from the temp branches

    Now you just need to find origin-master branch’s parent branch from all the temp branches (origin-master, origin-dev and origin-feature). It converts to the same situation as the post (Find the parent branch of a Git branch) shows. And you can use the same way to achieve.

  5. Convert the parent branch to the corresponding remote branch

    Such as you find the parent branch of origin-master branch is origin-dev, then replace - to / to get the remote parent branch name origin/dev.

  6. Delete all the temp branches

    git branch -D origin-master
    git branch -D origin-dev
    git branch -D origin-feature
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接