Git Pathspecs and Revision Separation
Separate revision from path
A pathspec is an operand that selects paths, potentially using patterns. The chapter fixture deliberately has both a topic branch and a tracked file named topic. git log topic alone is ambiguous here. Place a revision before -- and a file path after it. git log topic -- walks the topic branch history; git log -- topic filters current history for changes to the file, yielding B in this fixture.
git log --format=%s topic --git log --format=%s -- topicgit show B:topicQuote patterns so Git receives them
The shell can expand wildcards before Git sees them. Quote a pathspec intended for Git. With glob magic, docs/.md matches immediate Markdown files, and docs/**/.md also matches nested files. Without glob magic, wildcard matching rules differ; do not assume it behaves exactly like a shell glob or .gitignore. ls-files lists indexed paths, not all files on disk. The first command below returns two paths and the second four.
git ls-files -- ":(glob)docs/*.md"git ls-files -- ":(glob)docs/**/*.md"Use root, literal and exclusion selectors
The top magic anchors matching at the repository root even from a subdirectory; literal treats wildcard characters as ordinary characters; exclude subtracts matching paths after inclusion. Excluding docs/nested from docs leaves the two top-level Markdown files and keep.txt. Preview selection using read-only commands before using any broad pathspec with a state-changing command. -- separates revision/path operands in applicable commands; it is not the same as --end-of-options in rev-parse verification.
git ls-files -- docs ":(exclude)docs/nested"git -C docs ls-files --full-name -- ":(top)notes.txt"git ls-files -- ":(literal)topic"Practice: combine a range with a path
git log C..E -- notes.txt selects the relevant topic-side history for that path. The fixture returns D, not E, because E adds documentation only. Path-limited history may simplify merges; it is not necessarily a complete ancestry listing. Remove the pathspec when reasoning about the full graph. The next chapter edits private commit sequences, so first learn to select exactly the intended commits.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Pathspecs and Revision Separation — knowledge check
Predict revision resolution, commit sets and path selection.
Which command unambiguously selects the path topic in current history?
Why quote a glob pathspec?
Which magic anchors a pathspec at the root?
What does exclude magic do?
Which commit changes notes.txt in C..E in the fixture?