Complete Guide to Listing File Changes Between Two Git Commits

Nov 22, 2025 · Programming · 8 views · 7.8

Keywords: Git | file changes | version control | git diff | commit comparison

Abstract: This article provides a comprehensive guide on how to retrieve complete lists of changed files between two specific commits in Git version control system. Through the --name-only and --name-status options of git diff command, developers can efficiently generate file change reports to meet enterprise documentation and audit requirements. The article includes detailed command syntax, practical application scenarios, and code examples to help master core file change tracking techniques.

Overview of Git File Change Tracking Technology

In software development processes, generating code change reports is often necessary to meet project management, audit, or compliance requirements. Git, as the most popular distributed version control system, provides powerful file change tracking capabilities. Particularly in enterprise environments, developers need to accurately record and report code changes during specific time periods due to process standardization requirements.

Core Command Syntax Analysis

Git's git diff command is the core tool for file change analysis, offering flexible change tracking capabilities through different parameter combinations.

Basic File List Retrieval

To obtain a list of all changed file names between two commits, use the following command format:

git diff --name-only <commit1> <commit2>

Where <commit1> and <commit2> can be commit hashes, branch names, tags, or other valid Git references. For example, comparing two specific commits:

git diff --name-only abc123 def456

This command outputs file paths of all changes between the two commits, one filename per line, facilitating subsequent processing and analysis.

Advanced Options with Change Status

To understand the specific change type for each file, use the --name-status option:

git diff --name-status <commit1> <commit2>

This command adds change status identifiers before filenames:

Practical Application Scenarios Analysis

Comparison with Current Working Directory

In actual development, comparing a historical commit with the current working state is frequently needed. Use the following command:

git diff --name-only <starting SHA> HEAD

This command displays all file changes from the specified commit to the current HEAD commit. To include uncommitted local modifications, omit the HEAD parameter:

git diff --name-only <starting SHA>

Change Report Generation Practice

In enterprise environments, generating structured change reports is typically required. Here's a complete example workflow:

# Get file changes between recent two release versions
start_commit="v1.0.0"
end_commit="v1.1.0"

# Generate changed file list
echo "File Change Report: From ${start_commit} to ${end_commit}"
echo "================================"
git diff --name-status ${start_commit} ${end_commit}

Technical Details and Best Practices

Commit Reference Formats

Git supports various commit reference formats, including:

Output Processing and Automation

Change lists can be easily redirected to files or piped to other tools:

# Save to file
git diff --name-only commit1 commit2 > changed_files.txt

# Count number of changed files
git diff --name-only commit1 commit2 | wc -l

# View only specific file type changes
git diff --name-only commit1 commit2 | grep '\.java$'

Comparison with Other Commands

While git status can also display file changes, it primarily focuses on working directory and staging area status, whereas git diff --name-only is specifically designed for comparing differences between two specific commits, making it more suitable for generating historical change reports.

Conclusion

By properly utilizing git diff --name-only and git diff --name-status commands, development teams can efficiently generate accurate code change reports to meet enterprise process management and compliance requirements. The flexibility and powerful functionality of these commands make them indispensable tools in Git workflows.

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.