Git Diff

Updated on 28 Dec 2018

In this section we are interested in looking at the various methods of using git diff, and how we can tie that into the Git History plugin for VS Code.

To save on space, I’m going to focus on a particular section within a single file for comparison.

Your working copy and staging area

git diff

Your working copy and commit 57b50fae0 (or HEAD)

In this particular example I am interested in the action array. I have added 2 new parameters export and delete to action, but it hasn’t been committed yet. Note, I can replace 57b50fae0 with HEAD to compare my working copy with the latest commit.

git diff 57b50fae0 -- controllers/JobsController.php

Your working copy and master branch

In this example I want to see the difference between my working copy and the master branch.

git diff master -- README.md

The + / - is in reference to your current workspace.

master branch and feature1 branch

In this example I am looking at the difference between the master branch and feature1 branch. Lucky for me, I know that I added ‘I am on feature1’ to the feature1 branch - and this can be seen in the accompanying screen shot. .

git diff master..feature1 -- README.md

The + / - is in reference to feature1. If you only need to see a list of files that have changed, use the --name-only option.

git diff --name-only master..feature1

Compare commit a4d043027 and the latest commit

Similar to previous except this time I am comparing to the latest commit. view is in my working copy, but not committed. Therefore it shouldn’t show up when I do a git diff.

git diff a4d043027 HEAD -- controllers/ApitestController.php

Compare commit d09ba855 and commit 67aad037

Note that the format for the diff I have used is older commit compared with new commit. As expected, it is showing an extra parameter being added to the actions array.

git diff d09ba855 67aad037 -- controllers/ApitestController.php

Finding the diff in VS Code

If we open controllers/ApitestController.php in VS Code, we’ll be able to right click on any line in the file and see which commits modified that line. If you don’t already have it installed, install the Git History plugin for VS Code.

Once we click on Git View: Line History, we’ll see the commits where that line changed. Left click on the commit message and you’ll see a list of all the files that were changed on that commit.

Left click on a file and you’ll be presented with a list of options from the command palette.

You’ll be able to compare the file from that commit

  • with the previous version
  • with the one in your workspace.

In this instance I am comparing with the previous version.