@git / git-reference / commits / 687789b

git diff done

Scott Chacon committed Jun 4, 2010 at 06:02 UTC 687789b47f53cfa7d0ef081b157f2846619a0986
2 files changed +202 -13
basic/index.html
+186 -11
@@ -57,33 +57,33 @@ layout: reference
57 </p>
58
59 <pre>
60 -$ git status -s
61 -?? README
62 -?? hello.rb
60 +<b>$ git status -s</b>
61 +<span class="red">??</span> README
62 +<span class="red">??</span> hello.rb
63 </pre>
64
65 So right now we have two untracked files. We can now add them.
66
67 <pre>
68 -$ git add README hello.rb
68 +<b>$ git add README hello.rb</b>
69 </pre>
70
71 Now if we run <code>git status</code> again, we'll see that they've been
72 added.
73
74 <pre>
75 -$ git status -s
76 -A README
77 -A hello.rb
75 +<b>$ git status -s</b>
76 +<span class="green">A</span> README
77 +<span class="green">A</span> hello.rb
78 </pre>
79
80 <p>OK, so now if we edit one of these files and run <code>git status</code>
81 again, we will see something odd.</p>
82 <pre>
83 -$ vim README
84 -$ git status -s
85 -AM README
86 -A hello.rb
83 +<b>$ vim README</b>
84 +<b>$ git status -s</b>
85 +<span class="green">A</span><span class="red">M</span> README
86 +<span class="green">A</span> hello.rb
87 </pre>
88
89 <p>The 'AM' status means that the file has been modified on disk since we
@@ -184,6 +184,7 @@ A hello.rb
184 <span class="green">M</span><span class="red">M</span> README
185 <span class="red">D</span> hello.rb
186 </pre>
187 +
188 <p class="nutshell">
189 <strong>In a nutshell</strong>,
190 you run <code>git status</code> to see if anything has been modified
@@ -191,9 +192,183 @@ A hello.rb
192 commit a new snapshot and what will be recorded in it.
193 </p>
194
195 + </div>
196 +</div>
197 +
198 +<div class="box">
199 + <h2>
200 + <span class="docs">
201 + <a href="#">docs</a> &nbsp;
202 + <a href="#">book</a>
203 + </span>
204 + <a name="diff">git diff</a>
205 + <span class="desc">shows diff of what is staged and what is modified but unstaged</span>
206 + </h2>
207 +
208 + <div class="block">
209 + <p>There are two main uses of the <code>git diff</code> command. One use we
210 + will describe here, the other we will describe later in the
211 + <a href="/inspection">"Inspection and Comparison"</a>
212 + section. The way we're going to use it here is to describe the
213 + changes that are staged or modified on disk but unstaged.</p>
214 +
215 + <h4>
216 + git diff
217 + <small>show diff of unstaged changes</small>
218 + </h4>
219 +
220 + <p>Without any extra arguments, a simple <code>git diff</code> will display
221 + in unified diff format (a patch) what code or content you've changed in your
222 + project since the last commit that are not yet staged for the next commit
223 + snapshot.
224 + </p>
225 +
226 +<pre>
227 +<b>$ vim hello.rb</b>
228 +<b>$ git status -s</b>
229 + <span class="red">M</span> hello.rb
230 +<b>$ git diff</b>
231 +<span class="umber">diff --git a/hello.rb b/hello.rb
232 +index d62ac43..8d15d50 100644
233 +--- a/hello.rb
234 ++++ b/hello.rb</span>
235 +<span class="lblue">@@ -1,7 +1,7 @@</span>
236 + class HelloWorld
237 +
238 + def self.hello
239 +<span class="red">- puts "hello world"</span>
240 +<span class="green">+ puts "hola mundo"</span>
241 + end
242 +
243 + end
244 +</pre>
245 +
246 + <p>So where <code>git status</code> will show you what files have changed
247 + and/or been staged since your last commit, <code>git diff</code> will
248 + show you what those changes actually are, line by line. It's generally
249 + a good follow-up command to <code>git status</code>
250 + </p>
251 +
252 + <h4>
253 + git diff --cached
254 + <small>show diff of staged changes</small>
255 + </h4>
256 +
257 + <p>The <code>git diff --cached</code> command will show you what contents
258 + have been staged. That is, this will show you the changes that will
259 + currently go into the next commit snapshot. So, if you were to stage
260 + the change to <code>hello.rb</code> in the example above,
261 + <code>git diff</code> by itself won't show you any output because it will
262 + only show you what is <i>not yet</i> staged.
263 + </p>
264 +
265 +<pre>
266 +<b>$ git status -s</b>
267 + <span class="red">M</span> hello.rb
268 +<b>$ git add hello.rb </b>
269 +<b>$ git status -s</b>
270 +<span class="green">M</span> hello.rb
271 +<b>$ git diff</b>
272 +<b>$ </b>
273 +</pre>
274 +
275 + <p>If you want to see the staged changes, you can run
276 + <code>git diff --cached</code> instead.</p>
277 +
278 +<pre>
279 +<b>$ git status -s</b>
280 +<span class="green">M</span> hello.rb
281 +<b>$ git diff</b>
282 +<b>$ </b>
283 +<b>$ git diff --cached</b>
284 +<span class="umber">diff --git a/hello.rb b/hello.rb
285 +index d62ac43..8d15d50 100644
286 +--- a/hello.rb
287 ++++ b/hello.rb</span>
288 +<span class="lblue">@@ -1,7 +1,7 @@</span>
289 + class HelloWorld
290 +
291 + def self.hello
292 +<span class="red">- puts "hello world"</span>
293 +<span class="green">+ puts "hola mundo"</span>
294 + end
295 +
296 + end
297 +</pre>
298 +
299 + <h4>
300 + git diff HEAD
301 + <small>show diff of all staged or unstaged changes</small>
302 + </h4>
303 +
304 + <p>If you want to see both staged and unstaged changes together, you
305 + can run <code>git diff HEAD</code> - this basically means you want to
306 + see the difference between your working directory and the last commit,
307 + ignoring the staging area. If we make another change to our
308 + <code>hello.rb</code> file then we'll have some changes staged and some
309 + changes unstaged. Here are what all three <code>diff</code> commands
310 + will show you:</p>
311 +<pre>
312 +<b>$ vim hello.rb </b>
313 +<b>$ git diff</b>
314 +<span class="umber">diff --git a/hello.rb b/hello.rb
315 +index 4f40006..2ae9ba4 100644
316 +--- a/hello.rb
317 ++++ b/hello.rb</span>
318 +<span class="lblue">@@ -1,7 +1,7 @@</span>
319 + class HelloWorld
320 +
321 +<span class="green">+ # says hello</span>
322 + def self.hello
323 + puts "hola mundo"
324 + end
325 +
326 + end
327 +<b>$ git diff --cached</b>
328 +<span class="umber">diff --git a/hello.rb b/hello.rb
329 +index 2aabb6e..4f40006 100644
330 +--- a/hello.rb
331 ++++ b/hello.rb</span>
332 +<span class="lblue">@@ -1,7 +1,7 @@</span>
333 + class HelloWorld
334 +
335 + def self.hello
336 +<span class="red">- puts "hello world"</span>
337 +<span class="green">+ puts "hola mundo"</span>
338 + end
339 +
340 + end
341 +<b>$ git diff HEAD</b>
342 +<span class="umber">diff --git a/hello.rb b/hello.rb
343 +index 2aabb6e..2ae9ba4 100644
344 +--- a/hello.rb
345 ++++ b/hello.rb</span>
346 +<span class="lblue">@@ -1,7 +1,8 @@</span>
347 + class HelloWorld
348 +
349 +<span class="green">+ # says hello</span>
350 + def self.hello
351 +<span class="red">- puts "hello world"</span>
352 +<span class="green">+ puts "hola mundo"</span>
353 + end
354 +
355 + end
356 +</pre>
357 +
358 + <p>
359 + You can also provide a file path at the end of any of these options
360 + to limit the <code>diff</code> output to a specific file or subdirectory.
361 + </p>
362 +
363
364 + <p class="nutshell">
365 + <strong>In a nutshell</strong>,
366 + you run <code>git diff</code> to see details of the <code>git status</code>
367 + command - <i>how</i> files have been modified or staged on a line by line
368 + basis.
369 </p>
370
371 +
372 </div>
373 </div>
374
css/layout.css
+16 -2
@@ -40,6 +40,7 @@ p.nutshell {
40 margin-top: 10px;
41 padding: 8px;
42 background: #ffe;
43 + color: #555;
44 }
45
46 .box code.code {
@@ -51,9 +52,22 @@ p.nutshell {
52 margin: 10px;
53 color: #555;
54 }
54 -code.code b { color: #111; }
55 -.red { color: #833; }
55 +pre b { color: #111; }
56 +.red { color: #d33; }
57 .green { color: #383; }
58 +.umber { color: #8A3324; }
59 +.lblue { color: #55a; }
60 +
61 +.box h4 {
62 + font-family:monospace;
63 + margin-top: 20px;
64 +}
65 +.box h4 small {
66 + color: #888;
67 + font-size: 0.8em;
68 + margin-left: 10px;
69 + font-weight: normal;
70 +}
71
72 .note {
73 float: right;