How to Use Git Diff to Compare Changes Between Commits and Branches

How to Use Git Diff to Compare Changes Between Commits and Branches

git diff lets you compare changes between commits, branches, or specific files. It also offers options for summaries and detailed file changes.

Here are a few examples:

# Compare Changes Between Commits
git diff <commit1> <commit2>                   # Compare changes between two commits.
git diff <commit1> <commit2> -- <path/to/file> # Compare changes for a specific file between two commits.

# Compare Changes Between Branches
git diff <branch1> <branch2>                   # Compare changes between two branches.
git diff <branch1> <branch2> -- <path/to/file> # Compare changes for a specific file between two branches.

# Compare Working Directory with a Commit
git diff <commit-hash>                         # Compare the current working directory with a specific commit.
git diff <branch-name>                         # Compare the current working directory with a specific branch.

# Compare Changes Over Time
git diff HEAD@{1.day.ago}                      # Compare the current working directory with the state from 1 day ago.
git diff HEAD@{1.week.ago}                     # Compare the current working directory with the state from 1 week ago.

# Display Change Summary
git diff --stat <commit1> <commit2>            # Show summary of changes (files changed, lines added/removed).

# Display Changed File Names Only
git diff --name-only <commit1> <commit2>       # Show only the names of changed files between two commits.
git diff --name-status <commit1> <commit2>     # Show file names and their status (added, modified, deleted).

Subscribe to our newsletter to keep updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!