How to List Files Changed Between Two Commits in Git

When working with Git, there are times when you may want to check which files have been modified between two specific commits. A common scenario is needing to see the files changed between a particular commit with a specific message and the latest commit in your repository.

In this post, I’ll show you how to do this quickly and easily using Git commands. Let’s say you commit with messages like COMMIT_MESSAGE. Now, we want to find out what files have been changed between this commit and the latest commit (referred to as HEAD).

Step 1: Find the Commit Hash

The first step is to locate the commit that has the message COMMIT_MESSAGE. You can find this commit by using the following command:

git log –grep=”COMMIT_MESSAGE

This command will display a list of commits that have messages matching COMMIT_MESSAGE. Once you see the correct commit, take note of its hash (a long string of letters and numbers).

Step 2: List the Files Changed Between the Two Commits

After finding the commit hash, you can now list the files changed between that commit and the latest commit (HEAD) by using the following command:

git diff –name-only HASH HEAD

Replace HASH with the actual commit hash you found in the previous step. This command will list the files that have changed between the specified commit and the latest commit.

Explanation of Commands

  • git log --grep="message": This command searches through the commit history for commits that contain the given string in their commit message. The --grep option allows you to filter commits based on their message content.
  • git diff --name-only: This command compares two commits and shows the files that have been changed between them. The --name-only option limits the output to just the file names, without showing the details of the changes. If you want to see the actual differences in the files, you can run the git diff command without --name-only.