How do I diff the same file between two different commits on the same branch?

 

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 to do it with an example:

Suppose you have a Git repository, and you want to compare a file named example.txt between two different commits on the same branch.

  1. Find the Commit Hashes:

    First, find the commit hashes of the two commits you want to compare. You can use git log to list the commit history:

    bash
  • git log

    Note down the commit hashes of the two commits you're interested in.

  • Run the git diff Command:

    Use the git diff command to compare the same file (example.txt) between the two commits. Replace commit-hash-1 and commit-hash-2 with the actual commit hashes:

    bash
    1. git diff commit-hash-1 commit-hash-2 -- example.txt

      This command will show the differences between the contents of example.txt in the two specified commits.

    Here's an example with two arbitrary commit hashes:

    bash
    # Suppose commit-hash-1 is abc123 and commit-hash-2 is def456 git diff abc123 def456 -- example.txt

    The output of the git diff command will display the differences between example.txt in commit abc123 and commit def456. This allows you to see how the contents of the file have changed between the two commits on the same branch.

    Remember to replace commit-hash-1 and commit-hash-2 with the actual commit hashes you want to compare, and example.txt with the file path you want to diff.

    Comments