๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Viewing Differences in Git - git diff


Introduction

  • git diff command is used to view changes in files between various states in your Git repository.
  • It shows the differences between the working directory and the staging area, helping you to review the changes before committing.
  • It is useful for code reviews and debugging.
  • When to use git diff ?
    • To check the differences before committing changes.
    • To see changes between specific commits or branches.
    • To analyze conflicts during a merge process.

How to use git diff ?

  • View changes in files:
    • git diff
  • The command will show the following:
    • Git Log Command
  • Explanation of the Output:
    • diff --git a/aaa.txt b/aaa.txt
      • This line indicates that the file aaa.txt is being compared.
        • a/aaa.txt The file version from the staging area (previous state).
        • b/aaa.txt The file version from the working directory (current state).
    • index d9981b9..dced72b 100644
      • d9981b9 and dced72b are Git's internal hashes for the content before and after the change.
      • 100644 is the file permission mode.
    • --- a/aaa.txt
      • This marks the "old" version of the file.
    • +++ b/aaa.txt
      • This marks the "new" version of the file.
    • @@ -1 +1,2 @@
      • The -1 means starting at line 1 of the old version.
      • The +1,2 means starting at line 1 of the new version, with 2 lines in total.
    • -this is aaa first statement
      • The line prefixed with - indicates a line from the old version that was removed.
    • +this is aaa first statement
      +this is aaa second statement
      • The lines prefixed with + indicate lines added in the new version.
    • \ No newline at end of file
      • This message means that the file does not end with a newline character.

Different ways to use git diff ?

  1. View Unstaged Changes:
    • git diff
    • Shows changes between the working directory and the staging area.
    • Useful to check modifications before staging.
  2. View Staged Changes:
    • git diff --cached
    • Shows differences between the staging area and the last commit.
    • Useful to review changes before committing.
  3. Compare Working Directory with Last Commit:
    • git diff HEAD
    • Displays all changes since the last commit.
    • Useful to see all modifications, whether staged or not.
  4. Compare Specific Commits:
    • git diff commit1 commit2
    • Compares the differences between two commits.
    • Useful for tracking changes over time.
  5. View Changes for a Specific File:
    • git diff filename.txt
    • Shows modifications made to a specific file.
    • Useful for focused code reviews.
  6. Compare Staged and Unstaged Changes for a File:
    • git diff filename.txt
      git diff --cached filename.txt
    • Useful to compare what's modified but not staged vs. staged but not yet committed.
  7. Show Changes in a Specific Commit:
    • git diff commit-hash
    • Displays changes introduced by a specific commit.
    • Useful to understand the impact of a particular commit.
  8. Compare Changes Between Branches:
    • git diff branch1 branch2
    • Compares the differences between two branches.
    • Useful to check what will be merged or needs resolution.
  9. Ignore Whitespace Changes:
    • git diff -w
    • Ignores whitespace differences, focusing only on actual code changes.
    • Useful for cleaner comparisons.
  10. Show Summary of Changes:
    • git diff --stat
    • Shows a summary of the number of files changed, and lines added/removed.
    • Ideal for a quick overview of modifications.