0

I have a work assignment that requires me to rebase branches off of a specific branch only I can't remember which ones came off of that branch.

I've tried a few methods including:

  1. git log --first-parent
  2. git branch --contains
  3. git branch --contains develop | grep "^ *feature$"
  4. git log --graph --decorate as well as variations of git show branch... git show-branch -a
    | grep '*'
    | grep -v git rev-parse --abbrev-ref HEAD
    | head -n1
    | sed 's/[^[]*//'
    | awk 'match($0, /[[a-zA-Z0-9/-]+]/) { print substr( $0, RSTART+1, RLENGTH-2 )}'

What can I use?

  • 2
    The parent of a *branch* isn't well defined. "Parent", in Git terminology, refers one of the commits whose hash is stored in another commit. Multiple branches can result in multiple commits sharing a single parent commit, but no information is stored about which (if any) branch uniquely referenced the parent at the time a new branch is created. – chepner Nov 26 '21 at 14:59
  • The concept of *parent branch* can't properly be defined in git. – Nizar Zizoune Nov 26 '21 at 15:04
  • 1
    I'd go further than my estimated peer @chepner : no such thing as a parent branch in git. – Romain Valeri Nov 26 '21 at 15:04
  • @RomainValeri well we could say that branching out with a branch X from a branch Y makes Y a *parent branch* of X. How ever since branches are dynamic and can be rebased in a few seconds Y can totally differ from X. – Nizar Zizoune Nov 26 '21 at 15:07
  • 2
    I say "not well defined" because intuitively, if you currently have `foo` checked out and you create a new branch `bar`, you can consider `foo` to be the parent of `bar`. However, as time goes by, and some other branch `baz` branches off `foo`, it's no longer obvious if `foo` or `baz` was the original parent of `bar`, because the branch point between `bar` and `foo`/`baz` doesn't "know" whether `foo` or `baz` came first. – chepner Nov 26 '21 at 15:07
  • 1
    In git, only commits can be parents. *Parent branch* is a workflow concept, regardless of git, so git won't create/enforce *your* workflow if you don't. – Romain Valeri Nov 26 '21 at 15:24
  • In my workflow, when I need to rebase a branch, I inspect the commit tree with a graphical log viewer. There are plenty out there. The simplest command line one: `git log --all --graph --decorate --oneline`. Now, look at the graph and think about where the branch you need to rebase logically belongs. Nevermind where it came from, the important question is, what does it need to get combined with for the results to make sense? That's where you should rebase it onto. – joanis Nov 26 '21 at 21:47
  • 1
    Note that if you, personally, would like to record extra information *about* a branch name at the time you create that branch, you can write your own Git command that does this. Simply have the Git command create the branch and then run `git config branch.$branch.my-invented-thing $value`, where `$branch` is the branch you just created, and `$value` is the value you want to store. The key, `my-invented-thing`, is up to you: just pick something that nobody developing Git will pick in the future (this is of course impossible, so consider using your name or something here). – torek Nov 27 '21 at 02:42
  • 1
    Later, to retrieve the extra thing, figure out the branch's name (`git symbolic-ref --short HEAD`) and run `git config --get branch.$branch.my-invented-thing`. If it's been set, your value pops right back out. – torek Nov 27 '21 at 02:43

0 Answers0