To list the files that have changed between two commits in Git, get the SHAs of the commits and run:
git diff --name-only SHA1 SHA2
Alternatively, you can specify the start and end commits using the HEAD.
$ git diff --name-only HEAD~15 HEAD~8
This lists the files that have changed between 15th and 8th commit.
This comprehensive guide teaches you how to check the files that have changed between two commits. Besides, you’ll learn how to see other useful quick stats related to the changed files. You will see concrete examples of each command with a real Git project.
Let’s start by taking a look at an example
Example: What’s Changed between Commits
Let’s see a concrete example of what it looks like to check the file differences between commits.
Here’s my example project under Git version control:
And here’s the commit history.
Now, let’s see what files have changed between the most recent commit and the third-most recent commit:
$ git diff --name-only d8a0121 e8d6a1f
The output reveals that the files data.txt and test.txt have changed.
As another example, let’s check the changed files between the 5th most recent commit and the 2nd most recent commit using the HEAD:
$ git diff --name-only HEAD~5 HEAD~2
This is handy as I don’t need to copy-paste the SHAs of the commits but just count their positions and set the number after HEAD~.
Before you go, here are some useful tips you should consider when checking file changes between two commits.
4 Ways to See File Changes
Here are some alternative options you can use to get information about the files that have changed between two commits.
1. git diff –name-status
A very similar option to the –name-only is the –name-status. Besides naming the changed files, it quickly recaps what has happened to the file.
git diff --name-status SHA1 SHA2
$ git diff --name-status HEAD~4 HEAD~1 M data.txt M example.py M test.txt
This reveals that in my example project, the three files listed above were modified between the 4th and the 1st commit.
Here are all the –name-status flags:
||modified||The file was modified|
||copy-edit||The file was copied and modified|
||rename-edit||The file was renamed and modified|
||added||The file was added|
||deleted||The file was deleted|
||unmerged||The file has conflicts after a merge|
2. git diff –stat
When you run git diff with the –stat option, you’ll not only see the files that changed between commits but also the number of lines and additions/deletions.
git diff --stat SHA1 SHA2
This reveals that there were:
- 8 new lines added to the data.txt file.
- 1 new line to the example.py code file.
- 5 new lines to the test.txt file.
Git shows the number of added/removed lines as +/- signs. If the number of changed lines is greater than what fits on the screen, the number of +’s and -‘s are proportional to the number of changed lines.
data.txt | 250 +++–
This means 250 lines in total were changed, 150 added and 100 removed (3/5 of changes were added lines and 2/5 of the changes were removals proportionally.
3. git diff –shortstat
To get a short description of the changes between two commits in Git, you can do git diff –shortstat.
git diff --shortstat SHA1 SHA2
This prints a message like “N files changed, M insertions(+/-)“.
$ git diff --shortstat HEAD~4 HEAD~1 3 files changed, 14 insertions(+)
As the message suggests, this tells there were three files modified, and the modifications added 14 total lines to the files.
4. git show –name-only
To check what file changed between the recent and the second most recent commit in Git, you can do git show with the –name-only flag.
git show --name-only
Thanks for reading. Happy coding!
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
- Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
- Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)