title: Find the merge base between a remote and a commit
body: <p>Given a remote and a commitish, I want to merge it into a target branch on origin and generate a diff for it.</p>
  
  <p>If the target branch exists in origin, it's a simple call to <code>merge-base</code> to check if the remote commitish is related to origin's tree and to get a diff from the merge base to the remote commit.</p>
  
  <p>If it doesn't exist, blindly creating the new branch can be risky, as I want to make sure that the history being brought in is related to origin.</p>
  
  <p>Right now, I'm using <code>rev-list</code> to determine if the commit has a shared history with origin. Specifically, I'm using <code>git rev-list --max-parents=0 --remotes=origin</code> and <code>git rev-list --max-parents=0 committish</code> to get the root commit(s) for origin and the remote commit and checking that they have at least one shared root (<code>rev-list</code> can generate the union or difference between commits/remotes but not the intersection it seems).</p>
  
  <p>Now I know that the remote commit is related, but I still need to generate a diff. Right now, I only know what root commits in origin are reachable from the new commit. How can I find the best merge base in origin if I don't know which branch is the most closely related to the commit I want to bring in? Am I going about this completely wrong?</p>
  
  <p>In other words, given origin and commitish, how do I identify the merge base X given no knowledge of the hierarchy of origin? <code>git merge-base origin committish</code> is not valid since origin doesn't specify a single commit.</p>
  
  <pre><code>o----o----o master        \
        \                    }-- everything reachable from origin
         o---X---o branch2  /
              \
  o---o----o---o----o---o commitish    }-- new data from remote
  </code></pre>