Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git identify the most recent intersection of two branches

Tags:

git

Is there a shorthand for identifying the last commit common to two branches? For example if I have master and then topic1 splits off from master, and they both continue

master:
a------b------c------d
        \
topic1:  r------s------t

Is there a way to identify say b?
For example if there was such a thing as master#topic1 (same as topic1#master) that meant, "the most recent commit shared by both master and topic."

I would like to be able to:

$ git checkout topic1
$ git diff master#topic1..topic1

I know that I can do:

git diff master..topic

but I don't care about commits c and d.

like image 917
jpswain Avatar asked Jan 25 '11 03:01

jpswain


2 Answers

The answer to the question you're actually asking:

git diff master...topic

Note the three dots.

From the manpage:

git diff [--options] <commit>...<commit> [--] [<path>...]

This form is to view the changes on the branch containing and up to the second , starting at a common ancestor of both <commit>. git diff A...B is equivalent to git diff $(git merge-base A B) B. You can omit any one of <commit>, which has the same effect as using HEAD instead.

like image 110
Cascabel Avatar answered Sep 20 '22 18:09

Cascabel


use git merge-base.

NAME
git-merge-base - Find as good common ancestors as possible for a merge

SYNOPSIS
git merge-base [-a|--all] [--octopus] <commit> <commit>…
git merge-base --independent <commit>…
like image 33
J-16 SDiZ Avatar answered Sep 23 '22 18:09

J-16 SDiZ