找到一个LOCAL git分支的REMOTE父分支 [英] Find the REMOTE parent branch of a LOCAL git branch

查看:368
本文介绍了找到一个LOCAL git分支的REMOTE父分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取当前 当前提交的远程 git 分支的名称是分支式的吗?



我认为这是一个4步骤的过程,前三步可能如下所示:




第一步:获取当地分支的名称:

  git rev-parse --abbrev-ref HEAD 


 <第二步:从本地仓库获取当前签出的提交的散列:
code> git rev-parse HEAD#full hash


第三步:在远程git仓库中获取上游追踪分支的名称:
$ b

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


第四步:取名远程git repo上的父级上游跟踪分支:



需要执行哪些特定的代码?在阅读@ ChrisJohnsen对这篇帖子的回答后,我想象一下该解决方案涉及查找从远程存储库中的另一个分支派生的最近提交。但是,链接发布的答案中的代码似乎都是为本地存储库设计的。 这个当前的问题是不同的,因为我正在问如何从本地存储库子分支中找到远程存储库父分支。



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

解决方案

通过查看所有分支结构来获取远程父分支,可以打印提交历史记录如下图所示:

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

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


  1. git branch -r 列出所有远程分支$ c>(假设远程分支是 origin / master origin / dev origin /特性)。


  2. 从本地分支获取跟踪/远程分支

    在使用命令 git rev-parse --abbrev-ref @ {upstream} 时获取给定本地分支的远程分支的方式(假设它是 origin / master 要找到它的父分支的分支)

  3. 创建本地临时分支分支远程分支

    $ $ p $ g $ git分支-b起源主起源/主从本地创建一个本地分支原点$ master
    git branch -b origin-dev origin / dev
    git branach -b来源特征来源/特征


  4. 从临时分支中找到父分支



    现在您只需要找到 origin-ma从所有临时分支( origin-master origin-dev > 和o rigin-feature )。它转换为与帖子相同的情况(查找父级分支Git分支)显示。

  5. 将父分支转换为相应的远程分支



    如你发现 origin-master 分支的父分支是 origin-dev ,则替换 - / 来获得远程父分支名称 origin / dev 。删除所有临时分支

    $ $ $ $ g $ git分支

  6. D原始大师
    git分支-D原始开发者
    git分支-D原始特征



How do I get the name of the remote git branch from which the local git current commit was branched-off?

I assume this is a 4 step process, and that the first three steps might be as follows:


Step One: Get the name of the current branch in the local git repo:

git rev-parse --abbrev-ref HEAD    


Step Two: Get the hash of the currently checked out commit from the local repo:

git rev-parse HEAD          # full hash  


Step Three: Get the name of the upstream tracking branch on the remote git repo:

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


Step Four: Get the name of the parent upstream tracking branch on the remote git repo:

What specific code is required to do this? After reading @ChrisJohnsen's answer to this other posting, I imagine that the solution involves looking for the most recent commit that was derived from another branch in the remote repository. However, the code in the answers to the linked posting all seem to be designed for local repositories. This current question is different because I am asking how to find the remote repository parent branch from a local repository child branch.

I am adding the bash tag because these commands are running on CentOS servers that can use bash scripting.

解决方案

To get the remote parent branch by view all the branch structure, you can print the commit history as graph by any of below commands:

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

To get the remote parent branch only for a local branch by script, you can create local temp branches based on the remote branches. Then you can find the parent branch of the local branch from the temp branches. After finding the temp branch is the parent branch of the local branch, you can get the remote branch correspondingly. Finally, delete all the temp branches. Detail steps as below:

  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
    

这篇关于找到一个LOCAL git分支的REMOTE父分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆