What git branch did this branch respond?

Possible duplicate:
Find the parent branch of the branch

How do I know the name of the git branch, that the branch in question has split (if any)?

+5
source share
1 answer

It is easy for us to think that masteralways master, but my_branchalways my_branch, but this is not so. Let's say you have your repository on Github, your windows, your Linux and your office.

So you have 8 different branches:

github/master
github/my_branch
windows/master
windows/my_branch
linux/master
linux/my_branch
office/master
office/my_branch

You, as a person, see them as masterand my_branch, but git perceives them as 8 different branches. Therefore, if you have such a network:

------------------------------------------------ linux/master
 \--/ \-------/      /            \-------- office/my_branch
  | \---|--\--------/-------------------\------- my_branch
  |     |
  |   office/master
  | 
windows/master

What does it mean to ask, from where my_branch? This is the result of merging many branches!


, , . , . git log:

git log my_branch --pretty=oneline --graph

. git -log man:

--first-parent
    Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch,
    because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual
    commits brought in to your history by such a merge.

, . SHA1, :

git log my_branch --pretty=format:"%H" --first-parent

, , SHA1:

git branch --contains <commit>

script , script, SHA1, , , . . (: bash , ):

#! /bin/bash

if [ $# -lt 1 ]; then
  branch=master
else
  branch=$1
fi

sha1s=$(git log $1 --pretty=format:"%H")
res="Doesn't branch from anything"

for i in $sha1s; do
  b=$(git branch --contains $i | awk '{ if (NF > 1) print $2; else print $1 }') # use awk to remove * from current branch
  other_branch="";
  for j in $b; do
    if [ $branch != $j ]; then
      other_branch=$j
      break;
    fi
  done
  if [ -n "$other_branch" ]; then
    res=$other_branch
    break
  fi
done

printf -- '%s\n' "$res"

, , - . , my_branch master. :

                    /------------ master
------(master)-----
                    \------------ my_branch

. , . script , my_branch master , master my_branch. , .

+2

All Articles