The rewrite script might look like this (bash):
#!/bin/bash
count=0
for rev in $(git rev-list HEAD); do
num=$(git branch --all --contains ${rev} | wc | awk '{print $1}')
[ ${num} -eq 1 ] || break
count=$(( count + 1 ))
done
if [ ${count} -gt 0 ]; then
git rebase -i HEAD~${count}
fi
What I do a lot of times instead is (rebase from upstream point):
git rebase -i @{u}
The @{u}
technique does not catch other branches however.