title: Git commit commits staged and unstaged file
body: <p>TL;DR: When one file has staged and unstaged changes, a commit will commit both versions, with the most recent changes to the file. Why? I thought a commit would only commit the staged version, as noted here: <a href="https://stackoverflow.com/a/19892657/279516">https://stackoverflow.com/a/19892657/279516</a>.</p>
  
  <p>Let's say we have one file in our working directory. (It has previously been committed to the repo.)</p>
  
  <pre><code>$ ls
  foo.txt
  </code></pre>
  
  <p>The file's contents are currently just one character (the number 1):</p>
  
  <pre><code>$ cat foo.txt
  1
  </code></pre>
  
  <p>Let's change the contents of the file to be "12". Now we have this:</p>
  
  <pre><code>$ cat foo.txt
  12
  </code></pre>
  
  <p>Our working directory shows the change (instructional git output removed for brevity):</p>
  
  <pre><code>$ git status
  modified:   foo.txt
  </code></pre>
  
  <p>Now a <code>git add</code> will add that file to the staging index.</p>
  
  <pre><code>$ git add foo.txt
  </code></pre>
  
  <p>You can't see it here, but the file name is now green, to indicate it has been staged:</p>
  
  <pre><code>$ git status
  modified:   foo.txt
  </code></pre>
  
  <p>At this point, we could commit the file, and it would become part of our local repository. However, let's change <code>foo.txt</code> first and see what happens.</p>
  
  <pre><code>$ cat foo.txt
  123
  </code></pre>
  
  <p>And if we check <code>git status</code> we see <em>two</em> versions of foo.txt:</p>
  
  <pre><code>$ git status
  On branch master
  Changes to be committed:
  
          modified:   foo.txt
  
  Changes not staged for commit:
  
          modified:   foo.txt
  </code></pre>
  
  <p>The first <code>foo.txt</code> is green. The second is red. The first one has as its contents "12". The second one has "123". What happens if we commit now? Only the staged <code>foo.txt</code> should be committed. So the <code>foo.txt</code> with "12" as its body will be in our local repo. The other version of <code>foo.txt</code> is still just in our working directory.</p>
  
  <p>Commit <code>foo.txt</code> to our local repo, AS IT WAS WHEN WE ADDED IT:</p>
  
  <pre><code>$ git commit -m "Added 2 to foo." foo.txt
  </code></pre>
  
  <p>However, that's not what happened. Our working directory has no changes now. Both versions were committed. Why?</p>
  
  <pre><code>$ git status
  On branch master
  nothing to commit, working tree clean
  </code></pre>