Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was looking for the same thing, and I found this question. Thank you for asking it!</p> <p>However, I found that the answers I see here don't seem to <em>quite</em> give the answer you asked for (or that I was looking for) -- they seem to give the <code>G</code> commit, instead of the <code>A</code> commit.</p> <p>So, I've created the following tree (letters assigned in chronological order), so I could test things out:</p> <pre><code>A - B - D - F - G &lt;- "master" branch (at G) \ \ / C - E --' &lt;- "topic" branch (still at E) </code></pre> <p>This looks a little different than yours, because I wanted to make sure that I got (referring to this graph, not yours) B, but not A (and not D or E). Here are the letters attached to SHA prefixes and commit messages (my repo can be cloned from <a href="http://www.daveltd.com/misc/stackoverflow/oldest-common-ancestor.git" rel="noreferrer">here</a>, if that's interesting to anyone):</p> <pre><code>G: a9546a2 merge from topic back to master F: e7c863d commit on master after master was merged to topic E: 648ca35 merging master onto topic D: 37ad159 post-branch commit on master C: 132ee2a first commit on topic branch B: 6aafd7f second commit on master before branching A: 4112403 initial commit on master </code></pre> <p>So, the <strong><em>goal: find B</em></strong>. Here are three ways that I found, after a bit of tinkering:</p> <hr> <h2>1. visually, with gitk:</h2> <p>You should visually see a tree like this (as viewed from master):</p> <p><img src="https://i.stack.imgur.com/AVDtS.png" alt="gitk screen capture from master"></p> <p>or here (as viewed from topic):</p> <p><img src="https://i.stack.imgur.com/D4ZVU.png" alt="gitk screen capture from topic"></p> <p>in both cases, I've selected the commit that is <code>B</code> in my graph. Once you click on it, its full SHA is presented in a text input field just below the graph.</p> <hr> <h2>2. visually, but from the terminal:</h2> <p><code>git log --graph --oneline --all</code></p> <p><em>(Edit/side-note: adding <code>--decorate</code> can also be interesting; it adds an indication of branch names, tags, etc. Not adding this to the command-line above since the output below doesn't reflect its use.)</em></p> <p>which shows (assuming <code>git config --global color.ui auto</code>):</p> <p><img src="https://i.stack.imgur.com/OCZPM.png" alt="output of git log --graph --oneline --all"></p> <p>Or, in straight text:</p> <pre> * a9546a2 merge from topic back to master |\ | * 648ca35 merging master onto topic | |\ | * | 132ee2a first commit on topic branch * | | e7c863d commit on master after master was merged to topic | |/ |/| * | 37ad159 post-branch commit on master |/ * 6aafd7f second commit on master before branching * 4112403 initial commit on master </pre> <p>in either case, we see the 6aafd7f commit as the lowest common point, i.e. <code>B</code> in my graph, or <code>A</code> in yours.</p> <hr> <h2>3. With shell magic:</h2> <p>You don't specify in your question whether you wanted something like the above, or a single command that'll just get you the one revision, and nothing else. Well, here's the latter:</p> <pre><code>diff -u &lt;(git rev-list --first-parent topic) \ &lt;(git rev-list --first-parent master) | \ sed -ne 's/^ //p' | head -1 6aafd7ff98017c816033df18395c5c1e7829960d </code></pre> <p>Which you can also put into your ~/.gitconfig as <em>(note: trailing dash is important; thanks <a href="https://stackoverflow.com/users/681479/brian-white">Brian</a> for bringing attention to that)</em>:</p> <pre><code>[alias] oldest-ancestor = !zsh -c 'diff -u &lt;(git rev-list --first-parent "${1:-master}") &lt;(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\" | head -1' - </code></pre> <p>Which could be done via the following (convoluted with quoting) command-line:</p> <pre><code>git config --global alias.oldest-ancestor '!zsh -c '\''diff -u &lt;(git rev-list --first-parent "${1:-master}") &lt;(git rev-list --first-parent "${2:-HEAD}") | sed -ne "s/^ //p" | head -1'\'' -' </code></pre> <p>Note: <code>zsh</code> could just as easily have been <code>bash</code>, but <code>sh</code> will <em>not</em> work -- the <code>&lt;()</code> syntax doesn't exist in vanilla <code>sh</code>. (Thank you again, @conny, for making me aware of it in a comment on another answer on this page!)</p> <h3>Note: Alternate version of the above:</h3> <p>Thanks to <a href="https://stackoverflow.com/users/42610/liori">liori</a> for <a href="https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git/4991675#comment17222398_4991675">pointing out</a> that the above could fall down when comparing identical branches, and coming up with an alternate diff form which removes the sed form from the mix, and makes this "safer" (i.e. it returns a result (namely, the most recent commit) even when you compare master to master):</p> <p>As a .git-config line:</p> <pre><code>[alias] oldest-ancestor = !zsh -c 'diff --old-line-format='' --new-line-format='' &lt;(git rev-list --first-parent "${1:-master}") &lt;(git rev-list --first-parent "${2:-HEAD}") | head -1' - </code></pre> <p>From the shell:</p> <pre><code>git config --global alias.oldest-ancestor '!zsh -c '\''diff --old-line-format='' --new-line-format='' &lt;(git rev-list --first-parent "${1:-master}") &lt;(git rev-list --first-parent "${2:-HEAD}") | head -1'\'' -' </code></pre> <p>So, in my test tree (which was unavailable for a while, sorry; it's back), that now works on both master and topic (giving commits G and B, respectively). Thanks again, liori, for the alternate form.</p> <hr> <p>So, that's what I [and liori] came up with. It seems to work for me. It also allows an additional couple of aliases that might prove handy:</p> <pre><code>git config --global alias.branchdiff '!sh -c "git diff `git oldest-ancestor`.."' git config --global alias.branchlog '!sh -c "git log `git oldest-ancestor`.."' </code></pre> <p>Happy git-ing!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload