What You’ll Learn

What You’ll Need

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"

Original Zen

### ~ % cd ~/gitTest
### gitTest % ls -la
total 8
drwxr-xr-x   3 owner  staff    96 May 18 14:36 .
drwxr-xr-x@ 77 owner  staff  2464 May 18 14:36 ..
-rw-r--r--   1 owner  staff   857 May 18 14:31 zen
### gitTest % git init
Initialized empty Git repository in ~/gitTest/.git/

### gitTest % ls -la
total 8
drwxr-xr-x   4 owner  staff   128 May 18 14:37 .
drwxr-xr-x@ 77 owner  staff  2464 May 18 14:36 ..
drwxr-xr-x  10 owner  staff   320 May 18 14:38 .git
-rw-r--r--   1 owner  staff   857 May 18 14:31 zen

### gitTest % git add zen
### gitTest % git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   zen

Original

Original Zen

Modified

Changed Zen

Changed Zen

git diff shows the differences in two sections. The first has this header:

@@ -1,5 +1,5 @@

FirstChange

The “-1,5” means that git diff is showing us lines from the original file (-) starting at line 1, and it is showing us a total of five lines from the older version.

The “+1,5” means that git diff is showing us lines from the updated file (+) starting at line 1, and it is showing us a total of five lines from the updated version.

In this case, I simply replaced blank line 2 with “Sam Was Here!” The red minus sign (-) and the green plus sign (+) in the output are both referencing changed line 2. In the original, indicated by the minus sign (-), line 2 is blank. In the current version, indicated by the plus sign (+), we see the update to line 2: “Sam Was Here!” Another way to think about this is that the blank line 2 was removed (-) and that “Sam Was Here!” was added (+) as the new line 2.

Next, git diff shows us another section of the file indicated by:

@@ -12,10 +12,9 @@

OtherChanges

The “-12,10” means that git diff is showing us lines from the original file (-) starting at line 12, and it is showing us a total of 10 lines from the older version.

The “+12,9” means that git diff is showing us lines from the updated file (+) starting at line 12, and it is showing us a total of nine lines from the updated version. Note that the section marker “@@ -12,10 +12,9 @@” is appended to line 11 to give additional context.

Here, I removed the original line 15 and also modified the last line.

By default, git diff provides three lines of context before and after the changes it is identifying. This can be changed using the -U# flag.

U1 Context

U5 Context

Conclusion

You should now have a better understanding of the git diff output and hopefully be able to use it more effectively!

Learn More