Tuesday, 16 July 2019

Git diff: Show changes between commits, commit and working tree, etc


'git diff' command is used to see actual changes happened in the files.

Let’s see it with an example.

Step 1: Clone the repository 'https://github.com/harikrishna553/gitHelloWorld.git'
$git clone https://github.com/harikrishna553/gitHelloWorld.git
Cloning into 'gitHelloWorld'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 12 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
$
$ls
gitHelloWorld
$
$tree
.
└── gitHelloWorld
    ├── README.md
    └── welcome.txt

1 directory, 2 files


Step 2: Navigate to gitHelloWorld folder and execute ‘git diff’ command.
$cd gitHelloWorld/
$
$git diff
$

Since we are not done any changes to gitHelloWorld repository, ‘git diff’ command do not return anything.

Step 3: let's add new line to welcome.txt file.


welcome.txt
Hello, Welcome to git code repository training.

Add another line to welcome.txt file.

welcome.txt

Hello, Welcome to git code repository training.

Experimenting with 'git diff' command


Now execute ‘git diff’ command.
$git diff
diff --git a/welcome.txt b/welcome.txt
index 63bcf62..2810418 100644
--- a/welcome.txt
+++ b/welcome.txt
@@ -1 +1,3 @@
 Hello, Welcome to git code repository training.
+
+Experimenting with 'git diff' command

Explanation of git diff output
diff --git a/welcome.txt b/welcome.txt
Git comparing the files a/welcome.txt with b/welcome.txt. a/welcome.txt represents last snapshotted version of file, b/welcome.txt represents newly updated file.

index 63bcf62..2810418 100644
It is metadata of files. 63bcf62..2810418 are hashes or ids of files that are being compared. ‘100644’ is an identifier given to normal text files by git.

--- a/welcome.txt
+++ b/welcome.txt
File version a is assigned with – symbol and file version b is assigned with + symbol.

@@ -1 +1,3 @@
Specifies chunk changes. ‘+1,3’ specifies file version ‘b’ starts at line 1 and 3 lines modified.

Hello, Welcome to git code repository training.
+
+Experimenting with 'git diff' command

Specifies the changes. ‘+’ represents the changes in new file.


Previous                                                    Next                                                    Home

No comments:

Post a Comment