How to See the Differences Between Two Git Branches?

You can see the differences between two branches in git by using the "git diff" command, for example, like so:

git diff branch1 branch2

This command will compare the differences between the two branches and display the changes made in "branch2" (the target branch) as compared to "branch1" (the source branch). It will show additions, deletions, and modifications to files, along with the corresponding line numbers, producing an output like the following for example:

$ git diff branch1 branch2

diff --git a/README.md b/README.md
index 1c5e292..5da8ed7 100755
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
Some content in README.md file.

-This line was removed in branch2.
+This line was added in branch2.

Some content in README.md file.

The output shows the following:

  1. diff --git a/README.md b/README.md:
    — indicates the file being compared (a/file.txt vs. b/file.txt).
  2. index 1c5e292..5da8ed7 100755:
    — shows the unique identifiers and file mode for the versions being compared.
  3. --- a/README.md:
    — indicates the path and name of the source/original version.
  4. +++ b/README.md:
    — indicates the path and name of the target/modified version.
  5. @@ -1,4 +1,4 @@:
    — hunk header indicating the range of lines in the original and modified versions.
  6. Some content in README.md file.:
    — unchanged line in source and target.
  7. -This line was removed in branch2.:
    — indicates a line removed in the target version.
  8. +This line was added in branch2.:
    — indicates a line added in the target version.
  9. Some content in README.md file.:
    — unchanged line in source and target.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.