Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Revisions and Pathspecs

Searching commit messages: :/text and ^{/text}

Reference for Git message search syntax: :/text from all refs, rev^{/text} from a starting point, regex rules, the ! negation and typical pitfalls.

Two forms let you name a commit by words from its message instead of by position. :/text finds the youngest commit reachable from any ref whose message matches text. rev^{/text} does the same search but starts walking from rev, so the answer depends on the branch you ask about.

The text is a regular expression (POSIX extended, as in git log --grep), matched against the full commit message, and the search goes from newest to oldest and stops at the first hit. Both forms are ordinary revisions, so they plug into git show, git reset, git rebase, git cherry-pick and git log.

Terminal
git show :/"fix login"
git show HEAD^{/"fix login"}
git rebase -i :/"start of the refactor"

Try it

Bash Find the newest matching commit

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 Searching from a branch and negating a match

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
:/<text>          # youngest commit from all refs whose message matches
<rev>^{/<text>}   # youngest commit reachable from <rev> whose message matches
<rev>^{/}         # same as <rev>, the empty pattern matches anything
:/!-<text>        # youngest commit whose message does NOT match
:/!!<text>        # a literal text beginning with an exclamation mark
Part Meaning
<text> POSIX extended regular expression matched anywhere in the message, not only the subject. Anchors ^ and $ apply per line.
!- Negation: select the first commit whose message does not match (documented for the :/ form).
!! Escape: starts a pattern that really begins with !.
<rev> Where the walk begins; the commit itself is considered first.

Errors and edge cases: if nothing matches you get fatal: ambiguous argument ':/text': unknown revision or path not in the working tree. With rev-parse --verify -q the failure is silent and only the exit status tells you. Other ! combinations are reserved and are rejected.

Usage notes

  • :/ is not path syntax. A colon followed by a slash searches messages. A name like :path or :2:path refers to the index; see rev:path and :N:path.
  • Matching is case sensitive, and the revision syntax has no flag to change that; use a class such as [Ff]ix. git log --grep does support -i.
  • Every ref counts for :/text. It looks at branches, tags, remote-tracking refs and HEAD, so a hit on someone else's branch can surprise you. Prefer HEAD^{/text} or branch^{/text} when you want an answer tied to a branch.
  • Walk order is by commit date. With clock skew or merged histories, "youngest" is the first one found in that ordering, which may not be the one you imagine.
  • Spaces need quoting. In a shell write :/"fix login" or ":/fix login", otherwise the shell splits the words.
  • Braces need quoting in some shells. ^{...} is fine in bash only inside quotes if it contains spaces; in zsh and PowerShell quote the whole revision.
  • Handy starting points: git rebase -i "HEAD^{/start of refactor}~1", or git reset --soft "HEAD^{/wip}~1" to squash everything since a marker commit.
  • Searches the message, not the diff. To find commits by what they changed, use git log -S or -G, covered in git log -S and -G.
  • Suffixes only work with the ^{/text} form. In :/text everything to the end of the string is the pattern, so :/typo~2 searches for the text typo~2. Write HEAD^{/typo}~2 to go two parents back from the match; see Caret and tilde.