Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
git log ^main feature      # same as main..feature
git log feature --not main # same again
git log --branches --not --remotes

Try it

Bash Caret, --not and the equivalent dot range

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.

Bash Several tips: work that is on no other branch

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

Terminal
^<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..B equals ^A B. Both give the commits in B that are not in A. The caret form scales to many exclusions: git log feature ^main ^release hides everything either of those has.
  • --not is a toggle that applies to what follows. In git log topic --not main hotfix, both main and hotfix are excluded. Repeat --not to switch back to including.
  • Unpushed work: git log --branches --not --remotes shows local commits that no remote-tracking branch contains. Add --oneline --decorate for a compact review before you push.
  • Unmerged branches: git log --branches --not main lists work on any local branch that has not reached main.
  • Symmetric ranges cannot be written with ^. A...B is the set of commits on either side but not both. With carets you would need A 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 create and git format-patch. For git bundle, an excluded tip makes the bundle incremental.
  • Performance: exclusions let Git stop walking early, so --not --remotes is cheaper than listing everything and filtering it yourself.