Skip to content

Instantly share code, notes, and snippets.

@joechrysler
Last active April 8, 2024 13:36
Show Gist options
  • Save joechrysler/6073741 to your computer and use it in GitHub Desktop.
Save joechrysler/6073741 to your computer and use it in GitHub Desktop.
Find the nearest parent branch of the current git branch
#!/usr/bin/env zsh
git show-branch -a \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| sed 's/.*\[\(.*\)\].*/\1/' \
| sed 's/[\^~].*//'
# How it works:
# 1| Display a textual history of all commits.
# 2| Ancestors of the current commit are indicated
# by a star. Filter out everything else.
# 3| Ignore all the commits in the current branch.
# 4| The first result will be the nearest ancestor branch.
# Ignore the other results.
# 5| Branch names are displayed [in brackets]. Ignore
# everything outside the brackets, and the brackets.
# 6| Sometimes the branch name will include a ~2 or ^1 to
# indicate how many commits are between the referenced
# commit and the branch tip. We don't care. Ignore them.
@prachikhadke
Copy link

prachikhadke commented Mar 26, 2018

This script does not work. E.g. I merged a branch1 to master and then created branch2 from master. Ideally parent of branch2 is master, but this script always return branch1 as the parent which is not true. It is doing so because branches are all pointers and branch2 is pointing to the merge commit (from branch1 to master). even though that merge commit has 2 parents, only branch1 is returned.

@SrinivasRachakonda236
Copy link

It's not working for me, it is returning only one branch name as parent branch for all 800 branches in my repo I don't know why..?
But in reality it's not true, all branches have different parents. Any suggestions to get actual (nearest parent branch not ancestor parent branch) parent branch name of current branch.
Many Thanks...

@verdverm
Copy link

verdverm commented Jun 19, 2019

This did not work for me when I had done something like develop > release-v1.0.0 > feature-foo, it would go all the way back to develop, note there was a rebase involved, not sure if that is compounding my issue...

The following did give the correct commit hash for me

git log --decorate \
  | grep 'commit' \
  | grep 'origin/' \
  | head -n 2 \
  | tail -n 1 \
  | awk '{ print $2 }' \
  | tr -d "\n"

@jpbochi
Copy link

jpbochi commented Dec 11, 2019

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

The seds at the end are useful to transform something like origin/develop, origin/HEAD, develop into simply develop.

@alejandro-colomar
Copy link

alejandro-colomar commented Dec 15, 2020

Does the star in step 2 always come in the first char?
If so,

| grep '*' \

can be replaced by:

| grep '^*' \

which only searches for the star at the first position,
so it will be more precise.

@topunix
Copy link

topunix commented Jun 12, 2021

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

This did the trick for me, thanks!

@avatar-lavventura
Copy link

This does not return master branch name when master branch is the parent of the branch I check.

@JackHowa
Copy link

JackHowa commented Jan 27, 2022

git log --decorate
| grep 'commit'
| grep 'origin/'
| head -n 2
| tail -n 1
| awk '{ print $2 }'
| tr -d "\n"

This seems to work for me. However I'm not totally gathering why it wouldn't show return the latest commit that has 'origin' in the first couple lines of the current branch 🤔 @verdverm

https://explainshell.com/explain?cmd=git+log+--decorate+%7C+grep+%27commit%27+%7C+grep+%27origin%2F%27+%7C+head+-n+2+%7C+tail+-n+1+%7C+awk+%27%7B+print+%242+%7D%27+%7C+tr+-d+%22%5Cn%22

@verdverm
Copy link

verdverm commented Jan 28, 2022

@JackHowa we ended up recording the source branch at time of creation in a json file. This is exact and reliable. We have found many new and interesting uses for a "branch-config" (./bfg/*.json). Trying to figure it out from git was too troublesome.

@JackHowa
Copy link

that makes sense; we're leaning the same way. thanks @verdverm

@viveknuna
Copy link

@joechrysler It's not working for me on Windows 10, Could you please guide me?

@misku
Copy link

misku commented Oct 13, 2022

Thanks!

@eirnym
Copy link

eirnym commented Jan 28, 2023

In my case I see output as shown below, so all scripts above don't work correctly. Thus I enchanted a random script to show a correct branch. This is usually happening for me when I do cleanup and make many single-commit branches which I merge to the main one.

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

What I enchanced:

  1. first grep greps not only stas, but also the latest line
  2. second grep eliminates only branches with exact name. Match also includes cases like ^ and ~1231.
    Pattern doesn't use advantage of -E for better compatibility between various systems and grep implementations.
  3. few sed's at the end were collapsed into a single one, taking advantage of eagerness of * operator.

git show-branch outputs I have in my repositories:

$ git show-branch -a
! [main] test2
 * [t] test3
  ! [t2] test2
   ! [upstream/HEAD] Merge pull request #640 from repo/branch
    ! [upstream/main] Merge pull request #640 from repo/branch
-----
+     [main] test2
 *    [t] test3
 *    [t^] test2
 *    [t~2] test2
  +   [t2] test2
 *+   [t~3] test1
----- [upstream/HEAD] Merge pull request #640 from repo/branch
$ git show-branch   
! [main] test2
 ! [t] test3
  * [t2] test2
---
+   [main] test2
 +  [t] test3
 +  [t^] test2
 +  [t~2] test2
  * [t2] test2
 +* [t~3] test1
--- [main^] Merge pull request #640 from repo/branch

@geeksmith
Copy link

First, thank you for this gist -- it's exactly what I was looking for. I have two suggestions to improve it:

  1. To account for a branch commit distance larger than 9, the final sed command should be:
    's/[\^~][[:digit]]*//'
  2. The last two sed commands could be a done with a single sed invocation and two '-e' arguments, one for each command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment