Simple git diff Example
Here we construct the scenario presented in Figure 8-1, run through the scenario, and watch the various forms of git diff in action. First, let’s set up a simple repository with two files in it:
$mkdir /tmp/diff_example$cd /tmp/diff_example$git initInitialized empty Git repository in /tmp/diff_example/.git/ $echo "foo" > file1$echo "bar" > file2$git add file1 file2$git commit -m"Add file1 and file2"[master (root-commit)]: created fec5ba5: "Add file1 and file2" 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 file1 create mode 100644 file2
Next, let’s edit file1 by replacing the word “foo” with “quux”:
$ echo "quux" > file1The file1 has been modified in the working
directory but has not been staged. This state is not yet the situation
depicted in Figure 8-1, but you can still
make a comparison. You should expect output if you compare the working
directory with the index or the existing HEAD
versions. However, there should be no difference between the index and
the HEAD because nothing has been staged. (In other
words, what is staged is still the current
HEAD tree.)
# working directory versus index $git diffdiff --git a/file1 b/file1 index 257cc56..d90bda0 100644 --- a/file1 +++ b/file1 @@ -1 +1 @@ -foo +quux # working directory versus HEAD $git diff HEADdiff --git a/file1 b/file1 index 257cc56..d90bda0 100644 --- a/file1 +++ b/file1 @@ -1 +1 @@ -foo +quux # index vs HEAD, identical still $git diff --cached$
Applying the maxim ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access