{"id":396,"date":"2021-09-19T07:18:37","date_gmt":"2021-09-19T15:18:37","guid":{"rendered":"http:\/\/spacefold.com\/colin\/morethanfour\/?p=396"},"modified":"2021-09-19T12:34:46","modified_gmt":"2021-09-19T20:34:46","slug":"learning-git-appendix-d-whats-the-diff","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2021\/09\/19\/learning-git-appendix-d-whats-the-diff\/","title":{"rendered":"Learning Git &#8211; Appendix D: What&#8217;s the DIFF?"},"content":{"rendered":"\n<p>Ray is writing the next great American detective novel, and we&#8217;ve started by creating a remote BitBucket Git repository to manage his changes.<\/p>\n\n\n\n<p>Ray clones the repository:<\/p>\n\n\n\n<pre>$ git clone https:\/\/ray-chandler@bitbucket.org\/ray-chandler\/noir.git<\/pre>\n\n\n\n<p>Consider that Ray now has a tracking branch called &#8220;master&#8221;; a remote reference called &#8220;origin&#8221;; and a remote-tracking branch &#8220;origin\/master&#8221;.<\/p>\n\n\n\n<p>He has many possible versions of his novel:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <strong>working tree <\/strong>contains Ray&#8217;s manuscript with any current un-staged, un-committed changes;<\/li><li>The <strong>staging area <\/strong>may contain a copy that was saved for inclusion in the next commit (via <strong>add<\/strong>);<\/li><li><strong>HEAD<\/strong> is the most recent commit in the current branch (i.e. &#8220;master&#8221;);<\/li><li>The remote-tracking branch &#8220;origin\/master&#8221; may also have some un-merged differences from the last fetch operation;<\/li><li>The &#8220;master&#8221; branch in the remote repository on the server may have some un-fetched changes recently checked in by Ray&#8217;s editor.<\/li><\/ul>\n\n\n\n<p>Wow, that&#8217;s five possible versions.<\/p>\n\n\n\n<p><em>Scene: Ray sips at a glass of bourbon, and types furiously, making changes to Chapter 1. He saves his work periodically.<\/em><\/p>\n\n\n\n<p>While Ray is accessing his muse, let&#8217;s learn about Git&#8217;s <strong>diff<\/strong> tool:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using DIFF<\/h3>\n\n\n\n<p>The Git <strong>diff<\/strong> command is a tool we can use to compare different versions of source files:<\/p>\n\n\n\n<pre>$ git diff &lt;target&gt; &lt;source&gt; # <\/pre>\n\n\n\n<p>This produces a report of changes needed to make &#8220;target&#8221; look like &#8220;source&#8221;.  There are some more common, special cases:<\/p>\n\n\n\n<p>To make &lt;target&gt; look like the <strong>working tree<\/strong>:<\/p>\n\n\n\n<pre>$ git diff          # target = staging area\n$ git diff HEAD     # target = HEAD     \n$ git diff &lt;target&gt; <\/pre>\n\n\n\n<p>To make &lt;target&gt; look like the <strong>staging area<\/strong>:<\/p>\n\n\n\n<pre>$ git diff --staged          # target = HEAD\n$ git diff --staged HEAD     # target = HEAD     \n$ git diff --staged &lt;target&gt;<\/pre>\n\n\n\n<p>What would  <strong>git commit -a<\/strong>  do? Find out with:<\/p>\n\n\n\n<pre>$ git diff --staged HEAD<\/pre>\n\n\n\n<p>What would  <strong>git commit<\/strong>  do? Find out with:<\/p>\n\n\n\n<pre>$ git diff --staged<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Examples<\/h4>\n\n\n\n<p>Consider the following sequence:<\/p>\n\n\n\n<pre>echo \"This line is pushed to remote\/master.\" &gt; source.txt\ngit commit -a -m \"Step1\"\ngit push origin\ngit switch -c secundo\necho \"This line is committed to secundo.\" &gt; source.txt\ngit commit -a -m \"Step2\"\ngit switch master\necho \"This line is committed to master.\" &gt; source.txt\ngit commit -a -m \"Step3\"\necho \"This line is in the staging area.\" &gt; source.txt\ngit add source.txt\necho \"This line is in the working tree.\" &gt; source.txt<\/pre>\n\n\n\n<p>We&#8217;ve now got different versions of source.txt in all possible locations. Let&#8217;s find out  the differences:<\/p>\n\n\n\n<pre>$ git diff<\/pre>\n<pre class=\"console-output\">diff --git a\/source.txt b\/source.txt\nindex 16f0b21..c260d9d 100644\n--- a\/source.txt\n+++ b\/source.txt\n@@ -1 +1 @@\n-This line is in the staging area.\n+This line is edited in the working tree.<\/pre>\n\n\n\n<p>Yup, that describes how to update the <strong>staging area<\/strong> to match the version in the <strong>working tree<\/strong>. (From here on out, I&#8217;ll omit the first few lines of the diff output).<\/p>\n\n\n\n<pre>$ git diff --staged origin\/master<\/pre>\n<pre class=\"console-output\">:\n-This line is pushed to remote\/master.\n+This line is in the staging area.<\/pre>\n\n<pre>$ git diff secundo master<\/pre>\n<pre class=\"console-output\">:\n-This line is committed to secundo.\n+This line is committed to master.<\/pre>\n\n\n\n<p>Exercise for the student: Try out the other variations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Back to that detective story<\/h3>\n\n\n\n<p><em>Cut scene: Editors office. Penny White is sitting at her computer.<\/em><\/p>\n\n\n\n<p>Penny cloned the repository yesterday, but she&#8217;s pretty sure they&#8217;ll be some updates from Ray on the server:<\/p>\n\n\n\n<pre>$ git fetch origin<\/pre>\n<pre class=\"console-output\">:\nUnpacking objects: 100% (6\/6), 738 bytes | 22.00 KiB\/s, done.\nFrom https:\/\/bitbucket.org\/ray-chandler\/noir\n   14c6528..713d32a  master -&gt; origin\/master<\/pre>\n\n<pre>$ git status<\/pre>\n<pre class=\"console-output\">On branch master\nYour branch is behind 'origin\/master' by 2 commits, and can be fast-forwarded.\n (use \"git pull\" to update your local branch)\nnothing to commit, working tree clean<\/pre>\n\n\n\n<p>Penny could <strong>merge<\/strong> at this point, but before she does, she&#8217;d like to see what the changes are.<\/p>\n\n\n\n<p>From our experiments above, we know that in order to see what will change during the merge, Penny will need to request a diff using &#8220;master&#8221; as the &lt;target&gt; and &#8220;origin\/master&#8221; as the &lt;source&gt;:<\/p>\n\n\n\n<pre>$ git diff master origin\/master<\/pre>\n<pre class=\"console-output\">diff --git a\/detective.txt b\/detective.txt\nindex df79c59..8616f4d 100644\n--- a\/detective.txt\n+++ b\/detective.txt\n@@ -1,7 +1,9 @@\n Chapter 1\n\n There was a knock at the door. I quickly hid the comic book under the WIRED magazine\n-and took out some official looking papers and scattered them about the desk.\n+on the desk and took out some official looking papers and scattered them about.\n\n \"Jess!\" I yelled.\n\n+The door opened. The siloette blocking the light from the door was a dame,\n+but it wasn't Jess.<\/pre>\n\n\n\n<p>Ray has been busy. The first thing Penny is going to do after merging is correct the spelling of &#8220;silhouette&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A Graphical Diff<\/h3>\n\n\n\n<p>Penny is not a fan of the text output from diff, so she has read up on the <strong>difftool<\/strong> command. It is essentially identical to <strong>diff<\/strong>, except Git will launch your preferred graphical utility to display the diff information.<\/p>\n\n\n\n<p>If you&#8217;re interacting with source code from inside an IDE that includes Git integration, this probably won&#8217;t be something you&#8217;ll need to do. But it helps to understand what is going on behind the scenes.<\/p>\n\n\n\n<p>I recommend <strong>meld<\/strong>, as it is reviewed positively and has versions for both Windows and Linux: <a href=\"https:\/\/meldmerge.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/meldmerge.org\/<\/a><\/p>\n\n\n\n<p>After we&#8217;ve installed it, we can enable it in Git by adding the following lines to .gitconfig:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;diff]\n    tool = meld\n&#x5B;difftool &quot;meld&quot;]\n    path = meld    ; Windows: c:\\\\Program Files (x86)\\\\Meld\\Meld.exe\n&#x5B;difftool]\n    prompt=false\n\n&#x5B;merge]\n    tool = meld\n&#x5B;mergetool &quot;meld&quot;]\n    path = meld   ; Windows: c:\\\\Program Files (x86)\\\\Meld\\Meld.exe\n&#x5B;mergetool]\n    keepBackup = false\n<\/pre><\/div>\n\n\n<p>Penny views the changes using Meld:<\/p>\n\n\n\n<pre>$ git difftool master origin\/master<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"309\" src=\"http:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image-1024x309.png\" alt=\"\" class=\"wp-image-400\" srcset=\"https:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image-1024x309.png 1024w, https:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image-300x91.png 300w, https:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image-768x232.png 768w, https:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image-676x204.png 676w, https:\/\/spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/2021\/09\/image.png 1350w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Using Meld to view the diff<\/figcaption><\/figure>\n\n\n\n<p>Meld presents each changed file in a separate window, opening them in sequence as the previous one is closed. For many files, this can get clumsy.<\/p>\n\n\n\n<p>An alternative would be to generate a list of files with differences, then launch the difftool for each file on your own schedule:<\/p>\n\n\n\n<pre>$ git diff --compact-summary master origin\/master<\/pre>\n<pre class=\"console-output\"> detective.txt   | 7 ++-----\n references.txt  | 1 -\n 2 files changed, 2 insertions(+), 6 deletions(-)\n<\/pre>\n\n<pre>$ git difftool master:detective.txt origin\/master:detective.txt<\/pre>\n\n\n\n<p>That last command demonstrates how Penny would specify a single file, using the &#8220;branch_path:file_spec&#8221; notation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Further reading:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/git-scm.com\/docs\/git-diff\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/git-scm.com\/docs\/git-diff<\/a>.<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/34119866\/setting-up-and-using-meld-as-your-git-difftool-and-mergetool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/34119866\/setting-up-and-using-meld-as-your-git-difftool-and-mergetool<\/a><\/li><\/ul>\n\n\n\n<p>That&#8217;s all for this Appendix. Go back to the article here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ray is writing the next great American detective novel, and we&#8217;ve started by creating a remote BitBucket Git repository to manage his changes. Ray clones the repository: $ git clone https:\/\/ray-chandler@bitbucket.org\/ray-chandler\/noir.git Consider that Ray now has a tracking branch called &#8220;master&#8221;; a remote reference called &#8220;origin&#8221;; and a remote-tracking branch &#8220;origin\/master&#8221;. He has many possible [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-source-control","post-preview"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}