Git

Git Diff Example

1. Introduction

As we all know, Git has been a very popular source code management system for software development. It has been widely used among programmers. Compared with SVN (Apache Subversion) and CVS (Concurrent Version System), Git is more powerful for its distributed nature, fast operation and branch handling mechanism. Because of the efficient of Git, Github, which is the web-based repository hosting service platform, is also popularly used.

In this article, Git, specifically Git Diff command will be discussed. All the examples below is shown in MacOS EI Capitan Version 10.11.3 and the Git version is 2.5.4. Note all the operations below is working on local machine, without connecting to Github.

2. Git Diff Example

2.1 Basic operation before going to Git Diff example

Before we go to the detailed example, maker sure git is installed on your computer/laptop. Follow the link here, you’ll fine the way to install Git successfully. Then in the terminal, try to run git --version, you will see the version of the Git you’ve installed as below:

WXMs-MacBook-Pro:~ WXM$ git --version
git version 2.5.4 (Apple Git-61)

After the successful installation of Git, we need to be able to handle the some basic operation of it.

Firstly, create a blank folder named GitDiffExample. Then go into the folder and type command git init. This command will change this folder to be a repository which you can manage. Also, the output after you type git init shows that you’ve successfully created and initialized an empty git repository. After running ls -ah, you will see there’s a .git folder inside the GitDiffExample folder. This folder is utilized to track the git folder. Make sure you won’t change it, leading to errors happening to the whole git folder. Note that .git folder is hidden by default, and you can use the previous command to make it visible.

For all these operations, you can check the result below:

WXMs-MacBook-Pro:~ WXM$ cd Documents/
WXMs-MacBook-Pro:Documents WXM$ cd GitDiffExample/
WXMs-MacBook-Pro:GitDiffExample WXM$ ls
WXMs-MacBook-Pro:GitDiffExample WXM$ git init
Initialized empty Git repository in /Users/WXM/Documents/GitDiffExample/.git/
WXMs-MacBook-Pro:GitDiffExample WXM$ ls -ah
.	..	.git

Secondly, we can use a text file as an example in usage of Git. Inside the GitDiffExample folder, create an empty txt file with name GitDiff.txt. Then add any words in it with: This is a txt file for Git Diff Example.

Lastly, we need to add the GitDiff file into the Git system. Then run git add GitDiff.txt. This command will add the txt file to the stage area, which is a middle area between workspace and the local repository. Then, we need to add this file from stage area to local repository. To achieve this, we can run git commit -m "First time writing the txt file". Note the string after -m is the comment that we make by ourselves. It can be anything you want to comment. Below shows the results of all these operations.

WXMs-MacBook-Pro:GitDiffExample WXM$ vi GitDiff.txt
WXMs-MacBook-Pro:GitDiffExample WXM$ ls
GitDiff.txt
WXMs-MacBook-Pro:GitDiffExample WXM$ cat GitDiff.txt 
This is a txt file for Git Diff Example.
WXMs-MacBook-Pro:GitDiffExample WXM$ git add GitDiff.txt 
WXMs-MacBook-Pro:GitDiffExample WXM$ git commit -m "First time writing the txt file"
[master (root-commit) 9f3fca6] First time writing the txt file
 1 file changed, 1 insertion(+)
 create mode 100644 GitDiff.txt

To make you more comfortable with the workspace, index, local repository, you can refer to an article here for further information.

Afterward, we can check the status of the whole folder with command git status. Then it seems to be everything if fine until now.

WXMs-MacBook-Pro:GitDiffExample WXM$ git status
On branch master
nothing to commit, working directory clean

2.2 Git Diff example

With familiar with the operations above, we’ll move to the main topic for git diff. The main purpose for this command is to show changes of the file between commits, commit and working tree, etc.

To make it work, we need to create some differences. As a start, we modify the content in the GitDiff.txt file and add one more line with “One more line”. The content in side the txt file now is:

This is a txt file for Git Diff Example.
One more line.

Then run command git diff, the output is:

WXMs-MacBook-Pro:GitDiffExample WXM$ git diff
diff --git a/GitDiff.txt b/GitDiff.txt
index 30e6813..8c7c8d2 100644
--- a/GitDiff.txt
+++ b/GitDiff.txt
@@ -1 +1,2 @@
 This is a txt file for Git Diff Example.
+One more line.

Basically the above output means that another line is added to the previous GitDiff.txt file. The “+” in the front shows which content has been added. The git diff will show the difference between the working directory and the index/staging area.

Another useful git diff command is git diff --cached, it’ll show the differences between the index and the most recent commit. After running this command in our example, it sows nothing. The reason for this, is that until now, there’s no difference between the index and most recent commit.

Then if we want to check the difference between the working directory and he most recent commit, git diff HEAD could be used. The output after running this command is:

diff --git a/GitDiff.txt b/GitDiff.txt
index 30e6813..8c7c8d2 100644
--- a/GitDiff.txt
+++ b/GitDiff.txt
@@ -1 +1,2 @@
 This is a txt file for Git Diff Example.
+One more line.

Here, we check the status of our current system, with the help of git status:

WXMs-MacBook-Pro:GitDiffExample WXM$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   GitDiff.txt

no changes added to commit (use "git add" and/or "git commit -a")

If we add the txt file to the index with command git add GitDiff.txt, then run the git diff, it shows nothing. That’s because right now the content in the working directory and the index are the same. This time, we run the git diff --cached, it shows the content below instead of showing nothing as previous.

WXMs-MacBook-Pro:GitDiffExample WXM$ git diff --cached
diff --git a/GitDiff.txt b/GitDiff.txt
index 30e6813..8c7c8d2 100644
--- a/GitDiff.txt
+++ b/GitDiff.txt
@@ -1 +1,2 @@
 This is a txt file for Git Diff Example.
+One more line.

To check the status, run git status:

WXMs-MacBook-Pro:GitDiffExample WXM$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	modified:   GitDiff.txt

Until now, there’s still difference between the index and the most recently commit. It can be proved by running git diff HEAD

WXMs-MacBook-Pro:GitDiffExample WXM$ git diff HEAD
diff --git a/GitDiff.txt b/GitDiff.txt
index 30e6813..8c7c8d2 100644
--- a/GitDiff.txt
+++ b/GitDiff.txt
@@ -1 +1,2 @@
 This is a txt file for Git Diff Example.
+One more line.

However, after we commit the file and rerun the previous command again, it shows nothing.

WXMs-MacBook-Pro:GitDiffExample WXM$ git commit -m "Add one more line"
[master cb3db9a] Add one more line
 1 file changed, 1 insertion(+)

Also, the status is clear right now, if we run the git status

WXMs-MacBook-Pro:GitDiffExample WXM$ git status
On branch master
nothing to commit, working directory clean

3. Conclusion

git diff command shows the difference between the files in working directory, index and most recent commit. The three most often used git diff commands could be:

  • git diff: Show differences between the working directory and the index.
  • git diff -–cached: Show differences between the index and the most recent commit.
  • git diff HEAD: Show the differences between your working directory and the most recent commit.

Jun Wu

Jun (Steven) Wu is a current Master student in Computer Science & Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button