git diff
outputgit diff
contextgit diff
to make a comparison.import this
command, or you can just copy it from this page.>>> 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!"
git init
command to create a local repository for that directory location. Once this is done, a hidden “.git” directory will be created. This is where Git stores all the revision control information.git add zen
. This tells Git that we want it to track changes made to this file.git status
.### ~ % 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
git diff zen
.git diff
shows the differences in two sections. The first has this header:
@@ -1,5 +1,5 @@
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 @@
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.
git diff -U1 zen
gives the following output with only one line of context around the changes:git diff -U5 zen
gives the following output with five lines of context around the changes:You should now have a better understanding of the git diff
output and hopefully be able to use it more effectively!
git diff --help