Basic git commands (Part 2)
Hello everyone! So, this is the second part of basic git commands. I have covered most of the starter friendly commands in part 1. In this blog we will be see git diff command where I'll be explaining the command though an example which is followed by an explanation on what each line does. Let's get started.
Understanding the concept via an example :
In the life cycle of git files, we know that it can be in 4 stages - untracked, staged, commited or modified. Say i created a file named fille1.txt which has some 4 lines of text in it, which I have added and committed. Now, I made some changes in this code and appended 2 more lines and successfully added it in staged area. Later, I made some changes in working directory and added 2 more lines.
Let's see the above in diagrammatic form:
Now, I want to know the differences in changes made to my file, and I want to see in each stage what version of my file is.
Git enables user to do this using git command - git diff. We can use git command to get the difference between a file at different stages.
git diff
To check the difference between working directory and staging area, we run above command on git. Let's consider the above example and see what happens when I run this command for file1.txt
diff --git a/file1.txt b/file2.txt
index 0313d69..196b75a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -4,3 +4,5 @@
4th line of file1.txt
5th line of file1.txt
6th line in file1.txt
+7th line in file1.txt
+8th line in file1.txt
Let's understand step by step what each line mean :
diff --git a/file1.txt b/file2.txt
In this line a/file1.txt is the source copy of our file (which will be the file from staging area) and b/file1/txt is the destination copy of our file.
index 0313d69..196b75a 100644
0313d69 is the hash of source copy, following that number 196b75a is the has of destination copy and 100644 tells us in which mode our file is first 3 number (100) tells the type and next 3 tells about file permission.
--- a/file1.txt +++ b/file1.txt
"---" followed by a/file1.txt says that source copy is missing some lines and "+++" followed by b/file1.txt says that destination copy has some new lines to it
@@ -4,3 +4,5 @@
- : source version
from 4th line source version has 3 more lines
+ : destination version
from 4th line destination version has 5 more lines
4th line of file1.txt 5th line of file1.txt 6th line in file1.txt +7th line in file1.txt +8th line in file1.txt
We can see that last 2 line is pre appended with '+' which says destination has 2 more new lines in it.
git diff command
The above example did help us understand how this git command works but what if we want to do find the difference between two files at different stages. So the below table contains the command for 8 cases , where each case is of 2 different stages of a file.
Source stage of a file | Destination stage of a file | git command |
staging area | working directory | git diff fileName |
last commit | working directory | git diff HEAD fileName |
last commit | staged area | git diff --staged HEAD fileName |
specific commit | working directory | git diff commitId fileName |
specific commit | staged area | git diff --staged commitId fileName |
source Commit | destination Commit | git diff sourceCommit destinationCommit |
second Last Commit | last Commit | git diff HEAD HEAD~1 fileName |
localRepo | remote Repo | git diff master (localRepo) origin (remote Repo) fileName |
sourceBranch | destination Branch | git diff sourceBranch destinationBranch fileName |
To compare all the files use git diff
without continuing the file name.
git difftool command
The above command will display the difference between files at different stages on git bash. To visualize these changes, we use p4merge tool.p4merge is a visual merge and diff tool developed by Perforce (CVCS).It is designed to help developers to visually compare, merge and resolve conflicts in source code and other text based files.
Now, you know to visualize these changes we have p4merge, but how to use it in Git?
We start by configuring the p4merge in Git.
- For difftool configuration :
git config --global diff.tool p4merge //sets defualt difftool
git config --global difftool.p4merge.path " " //set's up p4merge path
git config --global difftool prompt false //This is not mandatory, but setting prompt false will make sure that p4merge will open without asking permission.
These difftool commands are similar to diff commands just replace diff with difftool.
Subscribe to my newsletter
Read articles from Gautami Shetty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by