Reputation: 4533
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!
Upvotes: 109
Views: 50056
Reputation: 616
The only generic way I found so far.
git reflog show --no-abbrev $(git branch --show-current) | grep "branch: Created from" | awk '{print $1;}'
Upvotes: 4
Reputation: 6163
This will show you the first commit on topic
:
git rev-list topic ^master | tail -n 1
This will show the commit that the branch started from, i.e. the fork point:
git rev-parse "$(git rev-list topic ^master | tail -n 1)^"
These also work in the case where you have merged master
into topic
since creating the topic
branch. It assumes that topic
started from master
.
Explanation:
git rev-list topic ^master
Lists all commits on topic
that are not reachable from master
.
| tail -n 1
Then takes the last of those commits.
git rev-parse <rev>^
Prints the first parent of <rev>
.
Upvotes: 0
Reputation: 9529
If branches of interest are not yet merged, just like everyone says:
git merge-base branch1 branch2
If you've merged your branches (e.g. topic
and master
, merged either way):
git merge-base topic master
sha-of-last-merge
git rev-list --parents -n 1 sha1-of-last-merge
sha-of-last-merge sha-of-parent-1 sha-of-parent-2
git merge-base sha-of-parent-1 sha-of-parent-2
topic
branch from a commit on master
that had several parents (i.e. a merge commit), then no luck. Need heuristics / common sense / other knowledge.Upvotes: 3
Reputation: 341
The only 100% reliable way to do this is to tag the beginning of your branch when you create it. The accepted answer will not work if you merge commits back to the branch you originated your new branch from. This is sometimes done for example if you are creating a stabilizing release branch and want to merge back your fixes you find during release testing back to master. A common thing to do. If you know you will never merge commits from your new branch back to the originating branch then the accepted answer will work.
Upvotes: 11
Reputation: 265817
use git merge-base master your-branch
to find the best common ancestor between two branches (usually the branching point).
Upvotes: 99
Reputation: 34437
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.
Upvotes: 91