0

I did git checkout -b my_branch to create a new branch some days ago.

But funnily enough I do not remember now from which branch I created my_branch.

How can I find this?

I found this command:

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/' 

here: https://gist.github.com/joechrysler/6073741.

It gives back a reasonable output when I run but I want be sure that this is actually the right answer.

Does this command above work or there is another command?

Outcast
  • 4,967
  • 5
  • 44
  • 99
  • 1
    My understanding is that git doesn't track branches, it tracks commits. – Joshua Schlichting Apr 29 '20 at 16:49
  • 1
    Git does not store parent branches as a branch is just a reference to a commit. You can only tell if a branch is likely based of another branch. – dan1st Apr 29 '20 at 16:50
  • I would just use `git log --graph --all` – dan1st Apr 29 '20 at 16:51
  • 3
    Your best option is to track back, one commit at a time, until you find a commit that is also part of another branch. This is nowhere near foolproof, but *might* be enough for you. As others have commented, git does not in any way track which branch a commit was originally created on, nor which branch a new branch is created from. All you can do is try to work it out after the fact but there is no guarantee it will produce the right value. – Lasse V. Karlsen Apr 29 '20 at 16:52
  • @LasseV.Karlsen, it is not clear to me how this "Your best option is to track back, one commit at a time, until you find a commit that is also part of another branch" may give me a relevant answer. Can you please elaborate? – Outcast Apr 29 '20 at 18:10
  • and also @LasseV.Karlsen what do think about the command I posted above? Does it do what you are basically suggesting? – Outcast Apr 29 '20 at 18:11
  • Let me explain. If the current commit (head of your new branch) is *only* on your new branch, then it must be a commit you've made *on* that branch. So, you go to the parent of this commit and check again. If this commit is also only on your new branch, this has also been made on this branch. So you go the parent and check again. If this commit, however, is also part of another branch, *likely* this is where you created your new branch from. Not guaranteed, but possible. – Lasse V. Karlsen Apr 29 '20 at 20:07
  • Branches in Git don't have parent branches (perhaps they should, but they don't). See https://stackoverflow.com/q/3161204/1256452 for lots of ways to make guesses. – torek Apr 29 '20 at 20:51
  • @LasseV.Karlsen, ok thank you :) – Outcast Apr 30 '20 at 12:32

0 Answers0