Parker Coates Parker Coates - 2 months ago 6
Git Question

How do I automatically determine the best commit to interactively rebase from?

I frequently use "fixup!", "squash!" and "rebase -i" to clean up, correct, or just fiddle with recent, unpushed history. I'd like to find a way to get Git to automatically choose which commit makes the most sense for interactive rebasing, so that I could define an alias to do a quick interactive rebase.

I think the best commit in such cases would be the most recent ancestor to either:


  • appear in any other branch (local or remote)

  • have more than one parent

  • have no parents



The first condition ensures that I don't rewrite commits that might affect others. The second ensures that I don't try to rebase "through" a merge, which is definitely not what I want. The third is just a sanity check.

So the question is, how do I use Git find that commit?

Answer

So it's not pretty, but this is what I've come up with.

#! /bin/sh

commit=$(git rev-parse HEAD)

while [ "$(git log --format="%p" -1 $commit | wc -w)" -eq 1 ] \
      && [ "$(git branch --all --contains $commit | wc -l)" -eq 1 ]
do
    commit=$(git rev-parse "$commit^")
done

echo $commit

I'm sure there's a simpler, more robust way of accomplishing this, but it seems to do what I want. Suggestions and improvements are very much welcome. (It'd be nice to do this with just Git plumbing instead of relying on wc, for example.)