How to show Git log history i.e., all the related commits for a sub directory of a Git repository
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git can show the commit history for a subdirectory by combining git log with a pathspec. The core trick is to put -- before the path so Git knows you are filtering history by path rather than passing another revision argument.
Basic Directory History
The simplest command is:
You can also be more explicit:
This shows commits that affected files inside that subdirectory. A commit can still appear even if it also changed other parts of the repository, because Git is filtering by whether the commit touched the path, not by whether the commit was exclusive to that path.
Why the -- Matters
The -- separator tells Git that everything after it is a pathspec.
Without it, Git can misinterpret the path as a branch name, revision, or other argument, especially in repositories where names overlap.
Make the Output Easier to Scan
Directory history becomes easier to read with a compact format.
Or add author and date information:
This is useful when you are auditing ownership or investigating when a directory changed over time.
Show the Actual Changes Too
If you need the diff for each matching commit, add -p.
If you want a lighter summary, --stat is often easier to scan:
That shows which files in the subdirectory changed and how many lines moved without dumping the full patch for every commit.
Limiting the History
You can combine path filtering with all the normal git log options.
That makes it easy to answer narrow questions such as:
- who touched this area recently
- what changed before a release
- which commits from one developer affected this directory
Renames and Directory History Caveats
This is the subtle part. Git tracks content through commits, but directory rename history is not as straightforward as file rename history. For a single file, git log --follow -- file.txt can help across renames. For a directory, there is no direct equivalent that always reconstructs "the old name of this whole folder" automatically.
That means if a subdirectory was renamed, you often need to inspect older names explicitly.
For files inside the directory, --follow can still be useful one file at a time.
A Useful Investigation Pattern
When debugging a problem in one area of a monorepo, a good pattern is:
- start with
git log --oneline -- path/to/subdir - inspect a suspicious commit with
git show <commit> - narrow further to one file if needed
Example:
This is often faster than scrolling through full-repository history and mentally filtering irrelevant changes.
Common Pitfalls
The biggest pitfall is forgetting -- before the path. Git may still run, but the meaning of the command can change in confusing ways.
Another issue is expecting only directory-exclusive commits. Git shows commits that touched the path, even if they also modified files elsewhere in the same commit.
Developers also assume --follow works cleanly for entire directories. In practice, rename tracing is much more reliable for single files than for directory histories.
Finally, be careful with relative paths. Run the command from the correct repository root or give a path that makes sense from your current working directory.
Summary
- Use
git log -- path/to/subdirto show commits that touched a subdirectory. - Put
--before the path so Git treats it as a pathspec. - Add
--oneline,--stat, or-pdepending on how much detail you need. - Commits appear if they affected the directory, even if they also changed other files.
- Directory rename history is not as seamless as single-file
--followhistory, so older paths may need separate inspection.
Related reading
- How to show the first commit by 'git log'?
- How to show uncommitted changes in Git and some Git diffs in detail
- How to shrink the .git folder
- How to solve Permission denied publickey error when using Git?
- How to sort in-place using the merge sort algorithm?
- How to sparsely checkout only one single file from a git repository?
- How to specify the private SSH-key to use when executing shell command on Git?
- How to squash all commits on branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.