22

I am trying to find how to reference branch start commit from script. I mean the commit sha at which branch was forked.

Moreover I expect it work for history made from svn repo.

This post just gives first commit of repo creation and not feature branch start commit.

Community
  • 1
  • 1
MageSlayer
  • 421
  • 4
  • 12

1 Answers1

27

What you're looking for is the command merge-base:

git merge-base master feature-branch

will print the best common ancestor of those two branches, i.e. where they forked apart. (The documentation has pretty pretty pictures to clarify some of the interesting cases)

An additional tidbit is you can add the merge-base flag --fork-point to automatically include the name feature-branch, read more in this answer.

Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 2
    Note that if you've merged between your branch and the trunk at any point, a parent of that merge will be returned rather than the 'original' branch point. I'm not sure you'll get any better answer, though. – Andrew Aylett Mar 16 '10 at 21:29
  • 2
    @Andrew: It depends on the exact use, but generally that's what you want, anyway, not the original one. If you do really want the original one, you can use the `-a/--all` option and take the first one instead of the last one. – Cascabel Mar 16 '10 at 21:31
  • Big help, thanks! It leads to useful things like `git difftool \`git merge-base master HEAD\`` – Tim D Aug 07 '15 at 14:44
  • Why do you need to specify another branch?? The question was to find the starting commit for the current branch. This can only be 1 commit at the time it was created... – jaques-sam Jan 09 '19 at 14:53