Git Reference ยท Revisions and Pathspecs
Excluding revisions: ^rev and --not
Reference for the Git exclusion syntax: ^rev, --not and --all combinations to build ranges with several tips, such as 'everything not yet on any remote'.
A revision prefixed with ^ means "and not anything reachable from this". Together with ordinary revisions it builds a set of commits: everything reachable from the positive tips minus everything reachable from the excluded ones. The two-dot range A..B is just the common case of this rule.
The --not flag does the same job for a whole group. It flips the meaning of every revision that follows it, so --not --remotes says "exclude everything any remote branch knows about". Exclusion is how you ask the questions that dots cannot express: commits unique to one branch out of several, or work that has not been pushed anywhere.
git log ^main feature # same as main..feature git log feature --not main # same again git log --branches --not --remotes
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
^<rev> # exclude everything reachable from <rev> --not <rev>... # negate every revision that follows <rev> --not <rev>... # include the first, exclude the rest --not --not # a second --not turns negation off again
| Piece | Meaning |
|---|---|
^<rev> |
Marks <rev> and all its ancestors as excluded. |
--not |
Inverts the sense of the next revisions and of options such as --all, --branches, --tags, --remotes, --glob. |
--all, --branches[=<pattern>], --tags, --remotes |
Add every ref of that kind as a positive tip (negated after --not). |
--exclude=<glob> |
Skip refs matching the glob for the next --all, --branches, --tags or --remotes. |
Errors and edge cases: a lone ^main with no positive revision selects nothing, and git log ^main prints nothing. In a shell, ^ is special in some shells (older zsh setups, Windows cmd), so quote it as '^main' there.
Usage notes
A..Bequals^A B. Both give the commits inBthat are not inA. The caret form scales to many exclusions:git log feature ^main ^releasehides everything either of those has.--notis a toggle that applies to what follows. Ingit log topic --not main hotfix, bothmainandhotfixare excluded. Repeat--notto switch back to including.- Unpushed work:
git log --branches --not --remotesshows local commits that no remote-tracking branch contains. Add--oneline --decoratefor a compact review before you push. - Unmerged branches:
git log --branches --not mainlists work on any local branch that has not reachedmain. - Symmetric ranges cannot be written with
^.A...Bis the set of commits on either side but not both. With carets you would needA B --not $(git merge-base --all A B). - Merge bases:
^also works with--not $(git merge-base A B). Use git merge-base when you need the actual commit. - Not a pathspec.
^before a path means nothing for revisions; to exclude paths use pathspec magic such as:(exclude). - Commands that accept it: every command built on the revision walker, including
git log,git rev-list,git shortlog,git bundle createandgit format-patch. Forgit bundle, an excluded tip makes the bundle incremental. - Performance: exclusions let Git stop walking early, so
--not --remotesis cheaper than listing everything and filtering it yourself.