Comparisons In Git

In this article am going to tell you about, How to set up a Git Repository to demonstrating different types of comparison scenarios. There are Several types of comparisons which are listed as below:

  • Comparing the working directory and Staging area
  • Comparing Working directory and Git Repository(Last Commit)
  • Comparing Staging area and Git Repository(Last Commit)
  • Limiting comparisons to OneFile path
  • Comparison between the Commits
  • Comparing between Local and Remote Master Branches
  • Comparison section clean up and Push back to GitHub

Setting a Git Repository for Comparisons

First, we will set up our Git Repository to demonstrate different types of comparison scenarios.

  • For that, I am going to use the fork of my starter-web-1/ from scm-ninja.
    CS-starter-web-github
  • Let's go to the git terminal and then move to your project folder and then to starter-web-1/ Git Repository.
  • If you do an ls, you can notice that there is one file is missing called README.md.
    CS-ls

So let's create it by using the default text editor notepa++ README.md, as indicated by the.md, it's a markdown formatted file, which is a glorified text.

And in that, the Single pound(#) is the First level Heading and the Double pound(##) is the second level Heading

Now am going to add some text in the README.md file, am going to give heading as the starter web project and followed by Introduction, Purpose and How to Contribute and then save and exit.
CS-README-file

  • Now if you do a git status, you have your README.md file on your untracked list.
    CS-untracked-file
  • Now let's add README.md file to the staging area git add README.md
    CS-new-file
  • And commit this file git commit -m "Adding README.md File"
    CS-git-commit
  • Let's go ahead and push these changes back to the GitHub by using git push origin master
    CS-push
  • Now go to the browser and refresh it, you can see that the Github displays the content of the README.md file as formatted in HTML.
    CS-README-file-text
  • And if you go to the RAW, you can see the RAW version of the README.md file
    CS-raw-version
  • Do some changes to the README.md file by adding Deployment and then save and exit.
    CS-adding-deployment
  • Commit once again git commit -am "Adding new section Deployment"
    CS-git-commit-am
  • Do git status, Git tells us that we are on a clean working directory.
    CS-git-status
  • So push changes back to the Github, git push origin master
    CS-Git-push
  • Refresh the browser and you can see the deployment section in README.md file in Github.
    CS_contents-on github-deployment
  • Let us edit the file one more time by adding some text below Starter Web Project as This is a simple website project for showing how to use Git with Github Together save and then exit.
    CS-Notepad-content
  • Again do git status, it tells us that we have a modified file
    CS-modified
  • Now commit the modified file git commit -m "Adding description"
    CS-git-commit-one-more
  • Edit README.md for one more time notepad++ README.md and add some text below Introduction as This example is to show different parts of the git repository and various commands using a web project and then save and exit.
    CS-Notepad_content
  • Once again do commit git commit -m "Adding Introduction texts"
    CS-Adding-int-text
  • One more time edit README.md file and add some text below purpose as As stated above the main purpose is to provide simple examples for git save and then exit.
    CS-last-content
  • Instead of committing the changes, I am going to add this README.md file to the staging area by using git add README.md
    CS-git-add
  • Do a git status, so we have README.md file in a modified state in the staging area.
  • Let's modify the file again and add the text below the Deployment as This is a simple web project, deployment can be on any web server or even local file system save and then exit.
    CS-Notepad-content-of-deployment
  • One more time do a git status, it will tell that the README.md file is in the four main phases like in our local working directory in a modified state, staging area, in our local Git repository, that's already been committed and some commits are already in Github which represents the Remote side.
    CS-git-status-depl

Creating a Git Repository

Comparing Working Directory with Staging Area

The p4merge is actually very much useful in Comparing the different area of Git repository, what has been modified and what is there in the staging area.

As we know already that we have configured Git with Difftool command, hence by using the Difftool now, we can compare the Working directory with the staging area.

Let us see how it works:

First thing as I always used to tell you that, go to your project folder and check where are you exactly by using the pwd command and then go to your repository by using the cd command.

So here, in my case being in my project folder lets go to my repository cd starter-web-1/, If you do a git status, you can see that your working directory is not clean and you have modified the README.md file many times and hence your README.md file is in an unfinished state.
CS-git-status-depl

The git status command tells me that, the README.md file has been modified but it does not tell me what has been modified within that file, in order to get more details on that, we can use Git's Diff command.

If I type git diff without any parameter, git replies by comparing the difference between the working directory, that has been recently modified but not yet staged and whats currently staged.
WS-git-diff

Now press "q" and exit then you will be prompted back to the git terminal, as you know, in the previous article we have configured visual merge and difftool set up with Git.

So if I use difftool command then we can visually see the changes, Pass git difftool and press enter, Git will launch a visual difftool for us, so now we can easily compare and see the changes we have made.

On the right hand, is our working directory that has been modified recently and on the left-hand side is what has been currently staged.
WS-git-difftool

So I have my p4merge has a visual difftool, which requires me to completely quite before I return to the command prompt.

Once you close the visual merge dialogue box, Git will automatically take you to the Command prompt and then you can move further.

Working Directory

Comparing Working Directory and Git Repository(Last Commit)

We know the difference between the working directory and Git's staging area, Now let's see how our working directory compares to the last known commit.

  • Go to your project folder and check where are you exactly by using the pwd command and then go to your repository by using the cd command.
  • Then, If you do a git status, you can see that your working directory is not clean and you have modified the README.md file many times and hence your README.md file is in an unfinished state.
    CS-git-status-depl

So if we pass the command as git diff HEAD, using the pointer reference "HEAD" as a parameter, git will Compare the difference between the working directory and the Last Commit. We have added some couple of sentences in each section.
WR-git-diff-head

Let's do git difftool Head, git difftool command will use the same parameter as the git diff command, Now you can see with p4merge, the two sentences we have added at the different section of the README.md file

So again the right-hand side is the working directory and the left-hand side is a temporary file, that represents the state of the file on the last commit on the current branch
WR-git-difftool-head

Adding Git To An Existing Project

Comparing Staging Area with Git Repository

So far we have come to know how to compare the current working directory with the last known commit and also working directory changes with the staging area.

So let us know the difference between the what has been staged and the last commit on the current branch.

So by using git diff --staged HEAD, we may know the difference, Here we are comparing the staged area to the HEAD. of course, HEAD means the last commit on the current branch and at last press "q" to exit.
SG-git-diff-staged-head

So the above showed line is the last line which is added to the staging area, that is actually not present in the Git Repository.

Let us see the changes visually by using the command git difftool --staged HEAD , Well again, on the right side you can see our staged files and on left-side the last commit on the current branch.
SG-git-difftool-staged-head

Git Tracked Files

Limiting the Comparisons to OneLine or Path

Sometimes our Diffs involve multiple files in that and hence you can limit that search to just one file.

  • So let us set up this here if you do an ls, we can see our README.md file and also simple.html file.
    CL-ls
  • So let me modify the simple.html file within the title tag, so am going to give it as "Simple Title" as my page title, save then exit.
    CL-simple-text-file
  • If you do a git status, Git shows me that I have two changes, that are not staged yet, so the files are README.md file and Simple.html file, which I have modified just now.
    CL-git-status
  • To see those two files just give git diff , you will see README.md file changes and simple.html file changes are listed in the diff and then press "q" to exit.
    CL-readme-simple-file
  • Let us do a git diff to limit the diff to just README.md file by using the command git diff -- README.md
    CL-git-diff-readme
  • So now the diff returns the change which is involving in the README.md file.
  • And the same trick also works with the difftool command as well git difftool -- README.md
    CL-difftool-readme

Recursively Adding Files To Git Repository

Comparison Between the Commits

So far we know the differences between the working directory, staging area, and the last commit, now let us compare an arbitrary commit with the last commit.

  • To do that am going to use git's log command git log --oneline this will give us the history of the commits that have happened so far.
    CC-git-log-oneline

Let us consider the commit id in the middle and with the git diff command you can specify two arbitrary points to compare.

So git diff ref1 and ref2 where the ref1 is the commit id from the middle and the ref2 is the last commit id which is nothing but HEAD and press enter, you can see the difference between all the changes that had happened across several files from the last commit.
CC-git-diff-refone-reftwo

  • And to specify the commit, another syntax is there, git diff HEAD HEAD^ so, this command compares the HEAD and HEAD minus one.
    CC-git-diff-head-head
  • To see this visually, we can do git difftool HEAD HEAD^ so, now we can see the last commit on the branch and the commit just prior to the last commit.
    CC-git-difftool-head-head
  • So, once again am using the git log --oneline.
    CC-git-oneline-sec
  • Am going to compare git diff ref1 ref2 we can see the differences between the one and the other file and here we are not using the last commit id.
    CC_git-diff
  • We can try this by difftool by using the same ref1 and ref2, git difftool ref1 ref2 so you can see the one file on one side and the absence of the file on the other side and press "q" to exit.
    CC-git-difftool-refone-reftwo
  • So that the Git can feed the next file to the visual difftool and it has cycled through all the files which are different.
    CC-Loopinf-one
CC-looping-two

Moving and Renaming Files in Git

Comparison Between the Local and Remote Master Branches

In this part, am going to show you the difference between the two branches, Since we have not created any branch on the Git repository that currently we are working on.

So I am going to show you the difference between the Local Master branch and the Remote Master branch, where both are working the same.

To do that, am going to use the command git diff right side left side , where the right side refers to the master branch on the Github and the left side refers to the local branch on Git repository, and hence am using git diff master origin/master .
LR-git-diff-master-origin-master

You can observe that the above red color lines don't exist on the Git repository, they are the difference between the what's been committed in the master branch and what has been pushed up to Github.

So let us see this visually so you can see here that master is on the left side and the origin/master is on the right side.

Hence the right-hand side is the state of the file that is currently in Github versus the last commit on the master branch or else you can say it has HEAD and then press "q" to exist
LR-git-difftool-master-origin-master

  • If there are any files which are involved in the comparison, then it would just be cycled through it.

Setting up alias within Git

Comparison Section cleanup and Push back to Github

We are on the end part of the comparison section, So, let us clean up before we move on.

  • Let us do a git status, you can see that the working directory is not clean.
    CL-git-statusss
  • Go ahead and commit the changes you have done.
  • Before committing, add the file to the staging area git add .
    CL-git-add
  • Now commit git commit -m "updating repo with changes from compare section"
    CL-git-commit
  • Now, once again do a git status , Git tells us that we are on the clean working directory.
    CL-git-statusssssssssssss
  • The git commit message replies as we are ahead of 1 commit, let's move forward and update.
  • The best practice is to do git pull before pushing, as we expected it is already up-to-date.
    CL-git-pull
  • Let's push now, git push origin master
    CL-git-push
  • And go back to Github and reload the page, you can see the commit message that "updating repo with changes from compare section"
    CL-README-file-Commit-Message-ongithub
  • If you click on the README.md file, you can see all the changes we have made to the README.md file
    CL-contents-on-github





Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions