34

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.

4
  • 1
    not sure the questions are same but you can get answers here stackoverflow.com/questions/3161204/… Commented Jul 21, 2016 at 6:02
  • 2
    @Md.KhairulHasan The question is actually the same as you linked directly to this question again :) Commented Jul 21, 2016 at 6:03
  • You can only have decorated graphs, with that you can find, otherwise there is no direct command to find parent branch in git Commented Feb 21, 2023 at 6:06
  • Mandatory nitpicky reminder: There's no such thing as a parent branch in git. Only commits have parents, branches are just commit-labels. Commented Dec 17, 2024 at 10:19

9 Answers 9

18

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.

Sign up to request clarification or add additional context in comments.

1 Comment

FWIW, comparing against what the tool gitup shows is parent, this method is produces a correct result.
17

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^

3 Comments

FWIW, comparing against what the tool gitup shows is parent, this is incorrect.
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!
This command give another branch name, compared to the second line of git branch --contains
13

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/[\^~].*//'

1 Comment

FWIW, comparing against what the tool gitup shows is parent, this is correct.
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

Comments

1

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.

Comments

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

Comments

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.

Comments

0

With MacOs 15.0.1 (zsh) and Git 2.46.2, I use

git merge-base main $1 \
| xargs -I{} git branch --sort=creatordate --contains {} \
| grep -Ev "main|$1" \
| head -n 1
  • depending on the repository structure (assumig main is the default branch), merge base to main finds the last commit not (only) on the current branch
  • git branch lists all branches that commit is on, sorted by "oldest" (assuming your "core" branches are not overwritten all the time)
  • filter out the current branch and main (default) now presents the base branch first

Comments

0

Get the parent branch name of your current branch with this:

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

In Windows PowerShell I had to escape the expression.

git rev-parse --abbrev-ref "@{-1}"

The accepted answer by @andreas-wolf is more thorough. Where I use this, every branch is created with git checkout -b feature, then back into main, and merge if required.

From the Git Reference: gitrevisions

The construct @{-<n>} means the <n>th branch/commit checked out before the current one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.