How Do I Diff the Same File Between Two Different Commits on the Same Branch?

Better Stack Team
Updated on August 12, 2024

To diff the same file between two different commits on the same branch in Git, you can use the git diff command with the commit hashes and the file path. Here's how you can do it:

Steps to Diff the Same File Between Two Commits

  1. Identify the Commits: First, you need to know the commit hashes (SHA-1) of the two commits you want to compare. You can use git log to find these commits.
  2. Use git diff: Once you have the commit hashes, use the git diff command to compare the file between these commits.

Example

Assume you have a file named example.txt and you want to compare its content between two commits, commit1 and commit2.

Step 1: Find the Commits

Use git log to find the commit hashes. For example:

 
git log --oneline

Output might look like this:

 
abc1234 Commit message for commit2
def5678 Commit message for commit1

Here, abc1234 is the hash for commit2 and def5678 is the hash for commit1.

Step 2: Diff the File

Use the git diff command with the two commit hashes and the file path.

 
git diff def5678 abc1234 -- example.txt

This command will show the differences in example.txt between commit1 (def5678) and commit2 (abc1234).

Using Commit Hashes Directly

If you already know the commit hashes, you can directly use them with git diff:

 
git diff <commit1> <commit2> -- <file>

Replace <commit1> and <commit2> with the actual commit hashes and <file> with the file path.

Example Command

For the given example:

 
git diff def5678 abc1234 -- example.txt

Additional Tips

  • Comparing with HEAD: If you want to compare a file in the current working directory (HEAD) with an earlier commit, you can use:

     
    git diff <commit> -- <file>
    

    Example:

     
    git diff def5678 -- example.txt
    
  • Graphical Diff Tools: If you prefer using a graphical interface, many Git GUI tools (e.g., GitKraken, SourceTree, GitHub Desktop) provide options to compare files between commits visually.

Summary

To compare the same file between two different commits on the same branch:

  1. Identify the commit hashes using git log.
  2. Use the git diff <commit1> <commit2> -- <file> command to see the differences.

This method provides a clear view of how the file has changed between the two specified commits.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github