Git

How to List Only the Names of Files that Changed Between Two Commits

While working on the Git tracking tool, developers perform different operations on particular development project repositories, like creating, adding, and updating the source code files/folders for several purposes. Moreover, you can view the names of files that are changed/modified between two commits. To do so, Git offers the “$ git diff” command with the “–name-only” option.

In this blog, you will learn to show the file names that are modified between two commits.

How to List Only the Names of Files that Changed Between Two Commits?

To list only the names of particular files that changed between two commits, users are required to perform the below-listed steps:

  • Navigate to a desired local directory.
  • View the current repository list of content.
  • Open a particular file with the default text editor and make modifications.
  • Track and commit all added changes.
  • Check the name of files that changed between two commits by executing the “$ git diff –name-only <commit-hash1> <commit-hash2>” command.

Let’s try out the above-stated steps on your particular Git local repository!

Step 1: Navigate to Git Local Repository

First, move to the Git particular local repository by executing the “cd” command:

$ cd "C:\Users\nazma\Git\demo11"

Step 2: List Repository Content

Run the “ls” command to view the current repository content:

$ ls

Step 3: Open and Update Existing Text File

To open and update the existing text file, utilize the “start” command:

$ start file2.txt

Add some text, press the “CTRL + S“ key to save the changes, and close the file:

Step 4: Stage File

After adding modifications, track the added changes to the staging index through the following command:

$ git add .

Step 5: Add Changes to Repository

Update the Git repository using the “git commit” command along with the “-m” option and add the commit message:

$ git commit -m "second file updated"

Step 6: Start and Modify File Through Default Text Editor

Now, run the below-given command to open the file with the default text editor and update it:

$ start file2.txt

After updating the opened file, save changes and close it:

Step 7: Add Changes to Current Repository

Execute the following command to add all changes to the current repository and track them in the staging area:

$ git add .

Step 8: Commit Modifications/Changes

Commit changes, update, and save changes in the repository through the below-provided command:

$ git commit -m "second file re-updated"

Step 9: Check Git Repository Reference Log History

To view the Git repository reference log history, use the “git log .“ command:

$ git log .

Here, the below-highlighted SHA hash references are the most recently added commits:

Step 10: Show Changed File Name Between Two Commits

Lastly, run the git diff“ command with the “–name-only” option and particular commit SHA hashes:

$ git diff --name-only b8242fb 25fab4

Here, the “–name-only” option is used to display the name of the file which is changed between two commits:

It can be observed from the above output that file2.txt is changed between the specified commits.

Conclusion

To view the name of a file that changed between two commits, first move to the particular Git repository, and list the repository content. Then, open and update the file by adding some text. After that, save and track the changes to the staging index. Next, commit them to the repository and execute the “$ git diff –name-only <commit-hash1> <commit-hash2>” command. This blog provided the procedure to show the name of a file that changed between two commits.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.