How to Use Git Diff to Compare Branches or Commits: Step-by-Step Guide

Ever had to figure out what changed between two branches or commits, but struggled with tracking the exact differences?

We’ve all been there.

In this guide, let’s check out how to use Git Diff to easily compare branches or commits, so you can pinpoint those changes without hassle.

How to Use Git Diff to Compare Branches or Commits

Steps to follow:

1. Open 'Terminal' of your code editor.

2. Run command 'git branch' to list all branches of your repository.

3. Click 'Enter' to find the list of branches.

4. Use the 'git diff' command followed by the branch names to compare the differences between them.

5. This will display the changes found in '<branch_2>' that are not present in '<branch_1>'.

You can swap the branch names to view the reverse comparison.

6. You can also use commit hashes to compare the differences between two branches.

7. This will display the changes in '<commit 2>' that are not present in '<commit 1>'.

You can swap the commit hashes to view the reverse comparison.

Congratulations!

You've successfully learned how to use 'git diff' to compare branches and commits.

Conclusion

Now you can confidently track changes between branches or commits, knowing exactly what has been modified.

Git Diff simplifies this task and saves you from the pain of hunting down differences manually!

Still have questions?

Send an email to archana@leadwalnut.com, OR

Book a FREE consultation with an expert developer here.