Last Updated: November 19, 2020
·
105.2K
· pvdvreede

Find all files modified between commits in Git

There are many occasions where you may need to get a list of files that have changed between commit X and commit Y.

This is surprisingly easy with Git and can be done with the SHA, tag or relative to HEAD.

Use the following command:

git diff --name-only <SHA, tag start> <SHA, tag end>

This will use the diff command but will only output the file names rather than the changes within each file.

For example, we can get the names of all the files that have changed in the last 3 commits:

git diff --name-only HEAD HEAD~3

The above command will produce the following output:

dir/file1.rb
file.rb
another_dir/another_file.txt

You can then push that into a file or copy and paste it where you need.

You can also add the action that was performed on each file as well, so that you can see if the file was deleted, modified, or added:

git diff --name-status HEAD HEAD~3

This would produce the following output:

D           dir/file1.rb
M           file.rb
A           another_dir/another_file.txt

Where:

A = Added
D = Deleted
M = Modified
R = Renamed

Related protips:

git checkout remote branch