From 687789b47f53cfa7d0ef081b157f2846619a0986 Mon Sep 17 00:00:00 2001
From: Scott Chacon
-$ git status -s -?? README -?? hello.rb +$ git status -s +?? README +?? hello.rbSo right now we have two untracked files. We can now add them.
-$ git add README hello.rb +$ git add README hello.rbNow if we run
git status
again, we'll see that they've been
added.
-$ git status -s -A README -A hello.rb +$ git status -s +A README +A hello.rb
OK, so now if we edit one of these files and run git status
again, we will see something odd.
-$ vim README -$ git status -s -AM README -A hello.rb +$ vim README +$ git status -s +AM README +A hello.rb
The 'AM' status means that the file has been modified on disk since we @@ -184,6 +184,7 @@ A hello.rb MM README D hello.rb +
In a nutshell,
you run git status
to see if anything has been modified
@@ -191,9 +192,183 @@ A hello.rb
commit a new snapshot and what will be recorded in it.
There are two main uses of the git diff
command. One use we
+ will describe here, the other we will describe later in the
+ "Inspection and Comparison"
+ section. The way we're going to use it here is to describe the
+ changes that are staged or modified on disk but unstaged.
Without any extra arguments, a simple git diff
will display
+ in unified diff format (a patch) what code or content you've changed in your
+ project since the last commit that are not yet staged for the next commit
+ snapshot.
+$ vim hello.rb +$ git status -s + M hello.rb +$ git diff +diff --git a/hello.rb b/hello.rb +index d62ac43..8d15d50 100644 +--- a/hello.rb ++++ b/hello.rb +@@ -1,7 +1,7 @@ + class HelloWorld + + def self.hello +- puts "hello world" ++ puts "hola mundo" + end + + end ++ +
So where git status
will show you what files have changed
+ and/or been staged since your last commit, git diff
will
+ show you what those changes actually are, line by line. It's generally
+ a good follow-up command to git status
+
The git diff --cached
command will show you what contents
+ have been staged. That is, this will show you the changes that will
+ currently go into the next commit snapshot. So, if you were to stage
+ the change to hello.rb
in the example above,
+ git diff
by itself won't show you any output because it will
+ only show you what is not yet staged.
+
+$ git status -s + M hello.rb +$ git add hello.rb +$ git status -s +M hello.rb +$ git diff +$ ++ +
If you want to see the staged changes, you can run
+ git diff --cached
instead.
+$ git status -s +M hello.rb +$ git diff +$ +$ git diff --cached +diff --git a/hello.rb b/hello.rb +index d62ac43..8d15d50 100644 +--- a/hello.rb ++++ b/hello.rb +@@ -1,7 +1,7 @@ + class HelloWorld + + def self.hello +- puts "hello world" ++ puts "hola mundo" + end + + end ++ +
If you want to see both staged and unstaged changes together, you
+ can run git diff HEAD
- this basically means you want to
+ see the difference between your working directory and the last commit,
+ ignoring the staging area. If we make another change to our
+ hello.rb
file then we'll have some changes staged and some
+ changes unstaged. Here are what all three diff
commands
+ will show you:
+$ vim hello.rb +$ git diff +diff --git a/hello.rb b/hello.rb +index 4f40006..2ae9ba4 100644 +--- a/hello.rb ++++ b/hello.rb +@@ -1,7 +1,7 @@ + class HelloWorld + ++ # says hello + def self.hello + puts "hola mundo" + end + + end +$ git diff --cached +diff --git a/hello.rb b/hello.rb +index 2aabb6e..4f40006 100644 +--- a/hello.rb ++++ b/hello.rb +@@ -1,7 +1,7 @@ + class HelloWorld + + def self.hello +- puts "hello world" ++ puts "hola mundo" + end + + end +$ git diff HEAD +diff --git a/hello.rb b/hello.rb +index 2aabb6e..2ae9ba4 100644 +--- a/hello.rb ++++ b/hello.rb +@@ -1,7 +1,8 @@ + class HelloWorld + ++ # says hello + def self.hello +- puts "hello world" ++ puts "hola mundo" + end + + end ++ +
+ You can also provide a file path at the end of any of these options
+ to limit the diff
output to a specific file or subdirectory.
+
+ In a nutshell,
+ you run git diff
to see details of the git status
+ command - how files have been modified or staged on a line by line
+ basis.
+