File Comparison Using the Diff Command in Git

Written by artwalker | Published 2024/02/21
Tech Story Tags: git | github | diff-command | file-comparison | linux | shell-scripting | text-comparison | diff

TLDRYou use the diff command to find the differences between two files. The patch command is useful for applying file differences. The comparison is saved as a.diff file, which is then patched to the original file. Use the diff -u command to compare two files, line by line.via the TL;DR App

The diff command

You use the diff command to find the differences between two files. On its own, it’s a bit hard to use; instead, use diff -u to find lines that differ in two files:

Using diff -u

You use the diff -u command to compare two files, line by line and have the differing lines compared side-by-side in the same output. For an example of what this looks like, see below:

~$ cat menu1.txt 
Menu1:

Apples
Bananas
Oranges
Pears

~$ cat menu2.txt 
Menu:

Apples
Bananas
Grapes
Strawberries

~$ diff -u menu1.txt menu2.txt 
--- menu1.txt   2019-12-16 18:46:13.794879924 +0900
+++ menu2.txt   2019-12-16 18:46:42.090995670 +0900
@@ -1,6 +1,6 @@
-Menu1:
+Menu:
 
 Apples
 Bananas
-Oranges
-Pears
+Grapes
+Strawberries
  1. --- menu1.txt 2019-12-16 18:46:13.794879924 +0900: Indicates the filename and modification time of the first file.
  2. +++ menu2.txt 2019-12-16 18:46:42.090995670 +0900: Indicates the filename and modification time of the second file.
  3. @@ -1,6 +1,6 @@: Marks a block of differences, indicating where differences occur between the two files. -1,6 denotes six lines starting from the first line in the original file, and +1,6 represents the same in the modified file.
  4. -Menu1:: Represents the content in the original file. - signifies deleted lines.
  5. +Menu:: Represents the modified content in the new file. + signifies added lines.
  6. Apples, Bananas: These lines remain unchanged from the original file.
  7. Oranges, Pears: These lines from the original file were removed in the modified file.
  8. Grapes, Strawberries: These lines were added in the modified file.

The patch command

The patch command is useful for applying file differences. See the example below, which compares two files. The comparison is saved as a .diff file, which is then patched to the original file!

~$ cat hello_world.txt 
Hello World
~$ cat hello_world_long.txt 
Hello World

It's a wonderful day!
~$ diff -u hello_world.txt hello_world_long.txt 
--- hello_world.txt     2019-12-16 19:24:12.556102821 +0900
+++ hello_world_long.txt        2019-12-16 19:24:38.944207773 +0900
@@ -1 +1,3 @@
 Hello World
+
+It's a wonderful day!
~$ diff -u hello_world.txt hello_world_long.txt > hello_world.diff
~$ patch hello_world.txt < hello_world.diff 
patching file hello_world.txt
~$ cat hello_world.txt 
Hello World

It's a wonderful day!
  1. ~$ diff -u hello_world.txt hello_world_long.txt: This part represents the execution of the diff command to compare hello_world.txt and hello_world_long.txt.
  2. --- hello_world.txt 2019-12-16 19:24:12.556102821 +0900: This line shows the filename and the last modification time of the first file.
  3. +++ hello_world_long.txt 2019-12-16 19:24:38.944207773 +0900: This line shows the filename and the last modification time of the second file.
  4. @@ -1 +1,3 @@: This is a diff block marker, indicating the difference between the two files. -1 refers to the first line in the original file, and +1,3 indicates the first line to the third line in the new file.
  5. Hello World: This line represents the content in the original file.
  6. +: Signifies added content.
  7. It's a wonderful day!: This line represents the added content in the new file.


Written by artwalker | Passionate about Crafting Intelligent OS & Pioneering TinyML Applications for Resource-Constrained Devices
Published by HackerNoon on 2024/02/21