Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git how to find commit hash where branch originated from

Tags:

git

git-branch

Pretend I branched from my master branch to a topic branch, and then I did a few commits on my topic branch. Is there a command that tells me the commit hash on the master branch that my topic branch originated from?

Ideally, I wouldn't have to know how many commits I've made ( trying to avoid HEAD^5 ).

I've googled and SO'd around and can't seem to land on the answer. Thanks!

like image 736
brycemcd Avatar asked Oct 22 '10 16:10

brycemcd


People also ask

How do you know which branch is created from which branch?

Take a look at config file, perhaps there is branch. <branchname>. merge entry, which would tell you what branch this one is based on. You can also try git show-branch <branch> master , as an alternative.

How does git determine commit hash?

In Git, get the tree hash with: git cat-file commit HEAD | head -n1. The commit hash by hashing the data you see with cat-file . This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it's not the first commit.

What is commit hash in git?

The commit hash is an SHA-1 hash made up of a few properties from the commit itself. As mentioned above, it is a lot more complex than this post can go into, but understanding the fundamentals is a great first step. The git hash is made up of the following: The commit message. The file changes.


2 Answers

You can use git reflog show --no-abbrev <branch name>. It will output all changes made to the branch, including it's creation, for example (I created branch xxx from master branch):

bdbf21b087de5aa2e78a7d793e035d8bd9ec9629 xxx@{0}: branch: Created from master 

Note that this is not very reliable as reflog records can expire (90 days by default), and it seems like there is no 100% reliable way to do this.

like image 146
max Avatar answered Nov 02 '22 04:11

max


use git merge-base master your-branch to find the best common ancestor between two branches (usually the branching point).

like image 31
knittl Avatar answered Nov 02 '22 05:11

knittl