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.
git show :/"fix login" git show HEAD^{/"fix login"} git rebase -i :/"start of the refactor"
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
:/<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:pathor:2:pathrefers 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 --grepdoes support-i. - Every ref counts for
:/text. It looks at branches, tags, remote-tracking refs andHEAD, so a hit on someone else's branch can surprise you. PreferHEAD^{/text}orbranch^{/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; inzshand PowerShell quote the whole revision. - Handy starting points:
git rebase -i "HEAD^{/start of refactor}~1", orgit 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 -Sor-G, covered in git log -S and -G. - Suffixes only work with the
^{/text}form. In:/texteverything to the end of the string is the pattern, so:/typo~2searches for the texttypo~2. WriteHEAD^{/typo}~2to go two parents back from the match; see Caret and tilde.