git
version control
file changes
commit comparison
software development

How to list only the names of files that changed between two commits

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Git, keeping track of changes across various commits is crucial for effective version control and collaboration. A common task developers encounter is identifying what files have changed between two specific commits. This can be extremely helpful for code reviews, debugging, or simply understanding the evolution of a project.

In this article, we'll explore how to leverage Git's powerful command-line tools to list only the names of files that changed between two commits. With technical explanations, examples, and a summarized table, you'll gain a comprehensive understanding of this fundamental Git operation.

Technical Explanation

Git provides a rich set of commands to deal with differences between commits. The primary command that helps list file changes is git diff. A typical use case involves comparing two commit hashes, providing insights into file modifications across these states.

Understanding Commit Hashes

Every commit in Git has a unique SHA-1 hash. For example, a commit hash might look like ae3c89d4f0ab. These hashes act as identifiers for the state of your repository at particular points in time.

The git diff Command

The git diff command is used to display differences between commits, commit and working tree, etc. To list file names that have changed between two commits, the syntax is as follows:

 
git diff --name-only <commit1> <commit2>
  • --name-only: This argument ensures that only the names of the files are shown.
  • <commit1> and <commit2>: These should be replaced with the respective commit hashes.

Example

Assume you have a simple Git repository, and you want to list files that changed between commit ae3c89d and commit b8c9f7a.

bash
git diff --name-only ae3c89d b8c9f7a

This command will output a list of file names that have differences between the two specified commits.

Practical Scenarios

Let's explore some real-world scenarios where listing changed files can be particularly useful:

Code Reviews

When reviewing code, it can be beneficial to isolate changes file-by-file. Using git diff --name-only, you can quickly generate a list of files that require a deeper dive, ensuring you focus only on what matters.

Debugging

If a bug was introduced between two commits, identifying which files were modified within this range can narrow down potential causes.

Release Notes

When preparing release notes, knowing exactly which files were updated after the last release can aid in documenting features or fixes accurately.

Additional Tools

While git diff is the standard way to list changed files, several other tools and options can further enhance your workflow:

git log

Sometimes, you may wish to filter by commits that include specific file changes. The git log command can help:

bash
git log --name-only --oneline <commit1>..<commit2>
  • --oneline: Provides a summary of the commit message.
  • Output includes a list of the commit messages and file changes.

GUI Tools

For those who prefer graphical interfaces, applications like SourceTree, GitKraken, or GitHub Desktop also allow visualization of file changes between commits. While these tools provide the same data, many find it easier to navigate through GUI.

Summary Table

CommandPurposeExample
git diff --name-only a bLists file names changed between two commitsgit diff --name-only ae3c89d b8c9f7a
git log --name-only <range>Shows commits and names of changed filesgit log --name-only ae3c89d..b8c9f7a
GUI ToolsVisual representation of file changesSourceTree, GitKraken

Conclusion

Listing only the file names changed between two commits is a fundamental task in Git that can greatly aid in many development processes. Whether through git diff, git log, or GUI tools, understanding how to leverage these commands ensures a streamlined and effective approach to managing code changes. By mastering these techniques, developers can enhance their productivity and focus more on coding rather than tracking changes.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions