Programming

How to get a list of all files that changed between two Git commits

19 September 2026 · 8 min read

How to get a list of all files that changed between two Git commits

Managing code changes effectively is crucial for successful software development. Git, the widely-used version control system, provides powerful tools for tracking and managing these changes. One common task is to determine the specific files that have been modified between two different commits. Understanding how to get a list of all files that changed between two Git commits allows developers to pinpoint exactly what alterations have been made, aiding in code review, debugging, and understanding the evolution of a project. This knowledge is essential for maintaining code quality and collaborating effectively within a team. This detailed guide will walk you through the various methods and commands available in Git to achieve this goal, providing clarity and practical examples along the way. By mastering these techniques, you’ll improve your workflow and better understand the intricacies of your codebase.

Understanding Git Commits and Change Tracking

Git commits are essentially snapshots of your project at a specific point in time. Each commit contains metadata such as the author, committer, timestamp, and a unique identifier (SHA-1 hash). The core of Git’s functionality is its ability to track changes between these commits. When you modify a file and commit those changes, Git stores the differences between the previous version of the file and the new version. These differences are what allow you to revert to older states, branch your code, and collaborate with others. Understanding the relationship between commits is fundamental to using Git effectively.

Git uses a directed acyclic graph (DAG) to represent the history of commits. Each node in the graph represents a commit, and the edges represent the parent-child relationship between commits. This structure allows for branching and merging, which are essential for collaborative development workflows. When you ask Git for a list of files changed between two commits, you’re essentially asking it to compare the state of the project at two different nodes in this graph. This comparison reveals which files were added, modified, or deleted.

To effectively track changes, Git relies on a staging area (also known as the index). Before committing, you must explicitly add the changed files to the staging area using the git add command. This allows you to selectively choose which changes to include in a commit. According to the Git documentation, “The ‘index’ holds a snapshot of the content of the working tree, and it is this snapshot that becomes the contents of the next commit.” Git Internals - Git Objects This staging area ensures that your commits are atomic and represent a logical unit of work.

Using git diff to List Changed Files

The git diff command is a versatile tool for comparing different versions of files in your Git repository. It can be used to compare your working directory with the staging area, the staging area with the last commit, or two arbitrary commits. To get a list of all files that changed between two Git commits, you can use the following syntax:

git diff --name-only commit1 commit2

This command will output a list of filenames that differ between commit1 and commit2. The --name-only option ensures that only the filenames are printed, without the actual content differences. This is useful when you just need a quick overview of the files that have been modified. For example, if you want to see the files changed between commits a1b2c3d and e4f5g6h, you would run: git diff --name-only a1b2c3d e4f5g6h. The output would then be a simple list of filenames, such as:

  • src/main.java
  • README.md
  • config/application.properties

Another useful option is --name-status, which provides additional information about the type of change (added, modified, deleted, etc.). The output of git diff --name-status commit1 commit2 will include a status code before each filename, indicating the type of change. For example:

  • M src/main.java (Modified)
  • A new_file.txt (Added)
  • D deleted_file.txt (Deleted)

The git diff command is powerful and allows for flexible comparisons between commits, making it an essential tool for any Git user. According to a study by Atlassian, developers who use Git regularly report a 20% increase in productivity. Atlassian Git Tutorials: Comparing Commits This highlights the importance of mastering Git commands like git diff.

Using git log to Identify Changed Files

While git diff is excellent for comparing specific commits, git log can be used to browse the commit history and identify files changed in each commit. By combining git log with other options, you can effectively get a list of all files that changed between two Git commits within a specific range.

To view the files changed in each commit within a range, you can use the --name-only or --name-status options with git log. The syntax is as follows:

git log --name-only commit1..commit2

This command will display the commit messages along with the list of files changed in each commit between commit1 and commit2 (inclusive). The double dot .. specifies a range of commits. The featured snippet-optimized paragraph is the next one. To get only the list of files without the commit messages, you can combine it with other tools like awk or sed to filter the output. For instance, git log –name-only commit1..commit2 | grep -v ‘^commit’ | sort -u will provide a unique sorted list of changed files. This command first lists all files changed in the specified commit range, then removes lines starting with “commit” (which are commit headers), and finally sorts the remaining lines to remove duplicates, giving you a clean list of files that were modified between the two commits.

Here’s how to get a list of all files that changed between two git commits within a date range using git log. First you need to determine the commit hashes for the start and end dates using git log –before=“YYYY-MM-DD” –after=“YYYY-MM-DD” –pretty=%H -n 1. Then, use the git log –name-only <start_commit>..<end_commit> command to list the files changed between those commits. You can then filter and sort the output as described above. This allows you to focus on changes made within a specific timeframe.</end_commit></start_commit>

Step-by-Step Guide to Listing Changed Files

Let’s outline the exact steps to get a list of all files that changed between two Git commits using the methods described above. Here’s a clear, step-by-step guide:

  1. Identify the Commit Hashes: Determine the SHA-1 hashes of the two commits you want to compare. You can use git log to find these hashes.
  2. Use git diff –name-only: Execute the command git diff --name-only commit1 commit2, replacing commit1 and commit2 with the actual commit hashes. This will output a list of the filenames that have changed.
  3. (Optional) Use git diff –name-status: If you need information about the type of change (added, modified, deleted), use the command git diff --name-status commit1 commit2.
  4. (Optional) Use git log –name-only for a Range: If you want to see changes over a range of commits, use git log --name-only commit1..commit2.
  5. (Optional) Filter and Sort the Output: To get a clean, unique list of filenames from the git log output, use a command like git log --name-only commit1..commit2 | grep -v '^commit' | sort -u.

By following these steps, you can easily and efficiently identify the files that have changed between any two commits in your Git repository. These commands are fundamental to efficient code management and collaboration. Understanding the nuances of each command option, such as –name-only and –name-status, allows you to tailor your approach to specific needs. This ensures you get the precise information required, whether it’s a simple list of files or a detailed breakdown of changes.

Infographic here
FAQ: Listing Changed Files in Git ---------------------------------
How do I see the actual changes made to the files?
Use `git diff commit1 commit2` without the `--name-only` option. This will show the content differences between the files.
Can I compare changes between branches?
Yes, you can use `git diff branch1 branch2` to compare the latest commits on two branches.
How do I list files changed in the last commit?
Use `git diff --name-only HEAD^ HEAD`, where `HEAD` refers to the latest commit and `HEAD^` refers to the previous commit.
Is there a way to see the changes in a specific file between two commits?
Yes, use `git diff commit1 commit2 -- path/to/file` to see changes in a specific file.
These are just a few of the common questions that arise when working with Git and change tracking. Mastering these techniques will significantly enhance your ability to manage code effectively. Remember to consult the official Git documentation for more detailed information and advanced usage scenarios. [Official Git Documentation](https://git-scm.com/docs)

By now, you should have a solid understanding of how to get a list of all files that changed between two Git commits. The techniques described, including the use of git diff and git log, will empower you to efficiently track and manage code changes in your projects. Remember to practice these commands and explore their various options to become proficient in Git. Effective version control is critical for successful software development. To further enhance your Git skills, consider exploring branching strategies and conflict resolution techniques, ensuring a seamless workflow. You can also explore related topics on our blog or consult external resources for deeper insights.

Question & Answer :
Due to bureaucracy, I need to get a list of all changed files in my repository for a report (I started with existing source code).

What should I run to get this list?

For files changed between a given SHA and your current commit:

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

or if you want to include changed-but-not-yet-committed files:

git diff --name-only <starting SHA> 

More generally, the following syntax will always tell you which files changed between two commits (specified by their SHAs or other names):

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

Using --name-status instead of --name-only will show what happened to the files as well as the names.