Viewing Git Log History for Subdirectories: Filtering Commit History with git log

Nov 27, 2025 · Programming · 13 views · 7.8

Keywords: Git | Subdirectory Log | Commit History | Path Filtering | Repository Merging

Abstract: This article provides a comprehensive guide on how to view commit history for specific subdirectories in a Git repository. By using the git log command with path filters, developers can precisely display commits that only affect designated directories. The importance of the -- separator is explained, different methods are compared, and practical code examples demonstrate effective usage. The article also integrates repository merging scenarios to illustrate best practices for preserving file history integrity.

Methods for Viewing Subdirectory Commit History in Git

In large Git repositories, there is often a need to view commit history for specific subdirectories rather than all changes across the entire repository. This requirement is particularly common in scenarios such as modular development, code reviews, and issue troubleshooting.

Basic Command Syntax

To view commit history for a specific subdirectory, use the following command format:

git log -- path

The -- separator is crucial as it distinguishes path parameters from reference specifications (such as <since>..<until>). For example, to view commit history for the foo/A directory from the repository root:

git log -- foo/A

Practical Application Examples

Consider a repository containing a src/nvfs directory. Using the --oneline option provides a concise display of commit messages:

# Show changes for src/nvfs directory
git log --oneline -- src/nvfs
d6f6b3b Changes for Mac OS X
803fcc3 Initial Commit

# Compare with all changes in the repository
git log --oneline
d6f6b3b Changes for Mac OS X
96cbb79 gitignore
803fcc3 Initial Commit

The output comparison shows that the first command only displays two commits affecting the src/nvfs directory, while the second command shows all three commits in the repository.

Analysis of Alternative Methods

Another approach is to run the command directly within the target directory:

git log .

While this method is simple, it may be less precise than using the -- separator in certain situations, especially when combining with other git log options.

Integration with Repository Merging

When merging multiple Git repositories into one, preserving file history integrity is essential. The repository merging method described in the reference article:

# Create new repository and add remote repository
git init
git remote add -f old_a <repository URL>

# Merge and move files to subdirectory
git merge old_a/master
mkdir old_a
git mv * old_a/
git commit -m "Move files to subdirectory"

After merging, use git log -- old_a to view the complete commit history of the original repository within the subdirectory. For renamed files, use git log --follow <file> to track the complete history.

Best Practice Recommendations

1. Always use the -- separator to explicitly specify path parameters

2. Combine with options like --oneline and --graph for clearer output

3. Ensure proper escaping of path parameters when used in scripts

4. For merged repositories, use the --follow option to track file rename history

By mastering these techniques, developers can more efficiently manage and analyze code change history within Git repositories.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.