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