Thursday, April 29, 2021

Git diff command in detail

Diff command takes two inputs and reflects the differences between them. It is not necessary that these inputs are files only. It can be branches, working trees, commits and more. 


Below is an output 


diff --git a/test.html b/test.html

index 0b3f252..d629d58 100755

--- a/test.html

+++ b/test.html

@@ -178,8 +178,8 @@



  1. The first line shows the file names that have been considered as the input in git diff. You can see that they have been marked by a and b along with the two different file state that has been taken as input.
  2. This line is not of use. This shows the metadata related to the command and the execution of it on the files. this is the object hash value required by Git for internal use.
  3. This line defines the symbol, called a legend, to tell you what is used to describe the first file and what is used to describe the second file. As you can see, – is used in front of the first file and + is used in front of the second file. So whenever diff shows you the changes related to the first file, they will be marked by – and the changes in the second file will be marked by the symbol +.
  4. The fourth line shows you symbol @@ and symbols ahead of it. They are called chunks. Chunks in git diff define the change’ summary. In our image below the following chunk can be seen @@ -178,8 +178,8 @@
  5. This means that line 178 and 8 lines followed by it are displayed with the changes + a bit of context 



Chunk

A diff doesn't show the complete file from beginning to end: you wouldn't want to see everything in a 10,000 lines file, when only 2 lines have changed. Instead, it only shows those portions that were actually modified. Such a portion is called a "chunk" (or "hunk"). In addition to the actual changed lines, a chunk also contains a bit of "context": some (unchanged) lines before and after the modification so you can better understand in what context that change happened.

Chunk Header

Each of these chunks is prepended by a header. Enclosed in two "@" signs each, Git tells you which lines were affected. In our case the following lines are represented in the first chunk:

  • From file A (represented by a "-"), 6 lines are extracted, beginning from line no. 34
  • From file B (represented by a "+"), 8 lines are displayed, also starting from line no. 34

The text after the closing pair of "@@" aims to clarify the context, again: Git tries to display a method name or other contextual information of where this chunk was taken from in the file. However, this greatly depends on the programming language and doesn't work in all scenarios.


 

Changes

Each changed line is prepended with either a "+" or a "-" symbol. As explained, these symbols help you understand how exactly version A and B look: a line that is prepended with a "-" sign comes from A, while a line with a "+" sign comes from B.
In most cases, Git picks A and B in such a way that you can think of A/- as "old" content and B/+ as "new" content.

Let's look at our example:

  • Change #1 contains two lines prepended with a "+". Since no counterpart in A existed for these lines (no lines with "-"), this means that these lines were added.
  • Change #2 is just the opposite: in A, we have two lines marked with "-" signs. However, B doesn't have an equivalent (no "+" lines), meaning they were deleted.
  • In change #3, finally, some lines were actually modified: the two "-" lines were changed to look like the two "+" lines below.






References:

https://www.toolsqa.com/git/git-diff/

No comments:

Post a Comment