Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

git
version control

What is git diff branchname?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The diff function in Version control systems enables you to compare changes between two input data sources. The data sources can include files, branches, commits, and so on. The git diff command uses the above git data sources as input and executes the diffing function on them. The git diff branchname command allows you to compare the local version of the branch with the remote version.

Example

First, we open the gitexample project with a clean working directory. This means that all changes in the repository have been committed. The repository contains a file example.txt with the string “Hello World!”. Doing a git status gives us the following output:

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Let’s make changes to the example.txt file. We execute the following echo command in our working directory to change the contents of the file:

echo "Hello from Educative" > example.txt

Now, let’s execute the git diff main command to compare the local main branch and the remote origin/main branch:

git diff main

Output

diff --git a/example.txt b/example.txt
index 980a0d5..ad7204b 100644
--- a/example.txt
+++ b/example.txt
@@ -1 +1 @@
-Hello World!
+Hello from Educative

As we can observe, we can view the exact change that we made. Let’s break down the output in detail:

  • diff --git a/example.txt b/example.txt
    

    This line displays the two input data sources, a/example.txt and b/example.txt, to the git diff command.

  • index 980a0d5..ad7204b 100644
    

    This line displays some git metadata that is probably not useful to us.

  • --- a/example.txt
    +++ b/example.txt
    

    This line displays the markers for changes. - - - represents changes from a/example.txt and +++ represents changes from b/example.txt.

  • @@ -1 +1 @@
    -Hello World!
    +Hello from Educative
    

    The above lines display the list of diff chunks. Diff chunks refers to the sections of the file with changes. Our output contains only one diff chunk. The first line corresponds to the chunk header, in which -1 +1 indicates that changes have only been made to a single line in the file in our example. The remaining lines display the exact changes made.

RELATED TAGS

git
version control

CONTRIBUTOR

Abdul Monum
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring