The following shell command should do what you want:
git log --all --not $(git rev-list --no-walk --exclude=refs/heads/mybranch --all)
Caveats
If you have mybranch
checked out, the above command won't work. That's because the commits on mybranch
are also reachable by HEAD
, so Git doesn't consider the commits to be unique to mybranch
. To get it to work when mybranch
is checked out, you must also add an exclude for HEAD
:
git log --all --not $(git rev-list --no-walk \
--exclude=refs/heads/mybranch \
--exclude=HEAD \
--all)
However, you should not exclude HEAD
unless the mybranch
is checked out, otherwise you risk showing commits that are not exclusive to mybranch
.
Similarly, if you have a remote branch named origin/mybranch
that corresponds to the local mybranch
branch, you'll have to exclude it:
git log --all --not $(git rev-list --no-walk \
--exclude=refs/heads/mybranch \
--exclude=refs/remotes/origin/mybranch \
--all)
And if the remote branch is the default branch for the remote repository (usually only true for origin/master
), you'll have to exclude origin/HEAD
as well:
git log --all --not $(git rev-list --no-walk \
--exclude=refs/heads/mybranch \
--exclude=refs/remotes/origin/mybranch \
--exclude=refs/remotes/origin/HEAD \
--all)
If you have the branch checked out, and there's a remote branch, and the remote branch is the default for the remote repository, then you end up excluding a lot:
git log --all --not $(git rev-list --no-walk \
--exclude=refs/heads/mybranch \
--exclude=HEAD
--exclude=refs/remotes/origin/mybranch \
--exclude=refs/remotes/origin/HEAD \
--all)
Explanation
The git rev-list
command is a low-level (plumbing) command that walks the given revisions and dumps the SHA1 identifiers encountered. Think of it as equivalent to git log
except it only shows the SHA1—no log message, no author name, no timestamp, none of that "fancy" stuff.
The --no-walk
option, as the name implies, prevents git rev-list
from walking the ancestry chain. So if you type git rev-list --no-walk mybranch
it will only print one SHA1 identifier: the identifier of the tip commit of the mybranch
branch.
The --exclude=refs/heads/mybranch --all
arguments tell git rev-list
to start from each reference except for refs/heads/mybranch
.
So, when you run git rev-list --no-walk --exclude=refs/heads/mybranch --all
, Git prints the SHA1 identifier of the tip commit of each ref except for refs/heads/mybranch
. These commits and their ancestors are the commits you are not interested in—these are the commits you do not want to see.
The other commits are the ones you want to see, so we collect the output of git rev-list --no-walk --exclude=refs/heads/mybranch --all
and tell Git to show everything but those commits and their ancestors.
The --no-walk
argument is necessary for large repositories (and is an optimization for small repositories): Without it, Git would have to print, and the shell would have to collect (and store in memory) many more commit identifiers than necessary. With a large repository, the number of collected commits could easily exceed the shell's command-line argument limit.
Git bug?
I would have expected the following to work:
git log --all --not --exclude=refs/heads/mybranch --all
but it does not. I'm guessing this is a bug in Git, but maybe it's intentional.