28

Currently I am in a feature branch, but I don't know whether it was created from my develop or release branch.

Can anyone please tell me how I can get the parent branch name from the current git branch.

D Malan
  • 10,272
  • 3
  • 25
  • 50
Md. Naushad Alam
  • 8,011
  • 6
  • 25
  • 23
  • 1
    not sure the questions are same but you can get answers here http://stackoverflow.com/questions/3161204/find-the-parent-branch-of-a-git-branch – Md. Khairul Hasan Jul 21 '16 at 06:02
  • 2
    @Md.KhairulHasan The question is actually the same as you linked directly to this question again :) – DAXaholic Jul 21 '16 at 06:03
  • You can only have decorated graphs, with that you can find, otherwise there is no direct command to find parent branch in git – Saad Bilal Feb 21 '23 at 06:06

7 Answers7

14

Given that Git uses a directed acyclic graph and usually a repo only has one root, all your branches point back to one initial commit. So what you actually want is the branch that shares the largest part of its history with your branch.

You cannot just look for a branch whose HEAD is contained in your current branch’s history, as this branches HEAD will most likely have moved since then.

So I recommend you use git merge-base, which finds the newest common ancestor (aka fork point) of two branches or commits:

$ git merge-base feature develop
12345abc
$ git merge-base feature release
98765fed

This will output the two commits that appear in the history of both branches, respectively. One of the two will be contained in both branches, so you can feed them to merge-base again to get the commit you want (or rather, the one you don’t want):

git merge-base 12345abc 98765fed
98765fed

So in our example, feature was derived from develop, as the two share a commit that release does not have.

Note that this will only work if you don’t do criss-cross-merges between feature and develop.

Andreas Wolf
  • 995
  • 6
  • 19
  • FWIW, comparing against what the tool gitup shows is parent, this method is produces a correct result. – Joe Jul 08 '22 at 01:53
12

This one (from https://hankchizljaw.com/notes/24/) works for me:

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
TornadoAli
  • 2,065
  • 2
  • 19
  • 13
  • 1
    FWIW, comparing against what the tool gitup shows is parent, this is correct. – Joe Jul 08 '22 at 01:48
11

So far this worked for me. You can use this as terminal alias in Mac or any type of shortcut on Windows.

git log --pretty=format:'%D' HEAD^ | grep 'origin/' | head -n1 | sed 's@origin/@@' | sed 's@,.*@@'

As explained in many places, it is not a direct parent, it gives you nearest branch which from current branch is created created or shares same HEAD^

Narasimha
  • 759
  • 6
  • 8
  • 1
    FWIW, comparing against what the tool gitup shows is parent, this is incorrect. – Joe Jul 08 '22 at 01:49
  • This seems to be the only one that works so far. Other options that I have explored give me inconsistent results and lots of warnings. Thanks! – jonnyeom Apr 14 '23 at 14:49
1

I found this:

#!/bin/bash
git show-branch -a 2>/dev/null \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| perl -ple 's/\[[A-Za-z]+-\d+\][^\]]+$//; s/^.*\[([^~^\]]+).*$/$1/'

source: https://gist.github.com/joechrysler/6073741?permalink_comment_id=2391826#gistcomment-2391826

guettli
  • 25,042
  • 81
  • 346
  • 663
0

I got my answer by following this link.

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

Go to your project and open git bash(right click and select Git Bash Here) and past above command.

Dhaval Italiya
  • 386
  • 2
  • 7
0

Another way to phrase the question is:

What is the nearest commit that resides on a branch other than the current branch, and which branch is that?

This can be done with the command line command:

git show-branch \
| sed "s/].*//" \
| grep "\*" \
| grep -v "$(git rev-parse --abbrev-ref HEAD)" \
| head -n1 \
| sed "s/^.*\[//"

Answer taken from here: https://stackoverflow.com/a/17843908/8079979

Pranay Majmundar
  • 803
  • 7
  • 15
0

I work in a Git repo with thousands of branches, and when I wonder:

From which branch did I start my feature branch?

I know the answer will be one of a small set of branches. Also, the approach I use to determine the answer is (IMHO) conceptually a little easier to determine, using the 2-dot syntax of log.

Here's the bash alias that I use:

alias find-git-start-branch='for branch in \
  origin/next origin/develop origin/release origin/master origin/hotfix \
  ; do echo "Num commits not reachable by $branch: \
  $(git log $branch.. --oneline | wc -l)"; done'

(If you don't want the alias you can simply run the text between the single quotes.)

Line 2 is where I list the branches I wish to consider as the starting branch.

The meat of the script is all in the last line, and note that: git log origin/develop.. is just shorthand for git log origin/develop..HEAD.

That 2-dot syntax lists all commits on HEAD that aren't reachable by origin/develop, and that is exactly what you typically wish to know when determining from which branch you started from. Here's an example output when I run my alias against a branch I picked up after a day or two:

$ find-git-start-branch
Num commits not reachable by origin/next:   3
Num commits not reachable by origin/develop:   76
Num commits not reachable by origin/release:   86
Num commits not reachable by origin/master:   290
Num commits not reachable by origin/hotfix:   290

The smallest number represents the branch you started from, and should be the number of new commits on your branch, since then.

TTT
  • 22,611
  • 8
  • 63
  • 69