title: Git: commit partial changes
body: <p>There are multiple questions on Stack Overflow addressing the problem of staging and committing  only parts from files. However, I can't make it work.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/1085162/95592">How can I commit only part of a file in git</a></li>
  </ul>
  
  <p>Let's say we want to implement a dummy math class in php (the language doesn't matter) with some basic methods like: <code>add</code>, <code>subtract</code>, <code>multiply</code> and <code>divide.</code></p>
  
  <p>Let's start with the class definition:</p>
  
  <pre><code>&lt;?php
      class Math {
      }
  ?&gt;
  </code></pre>
  
  <p>Now:</p>
  
  <pre><code>$ git add math.php
  $ git commit -m "Create Math class."
  </code></pre>
  
  <p>In the next step we implement both <code>add</code> and <code>subtract</code> four methods:</p>
  
  <pre><code>&lt;?php
      class Math {
          public function add($a, $b) {
              return $a + $b;
          }
  
          public function subtract($a, $b) {
              return $a - $b;
          }
      }
  ?&gt;
  </code></pre>
  
  <p>But now we want to commit the implementation of the <code>add</code> and the <code>subtract</code> methods in separate commits.</p>
  
  <p>Is this possible?</p>
  
  <p><strong>What I tried</strong></p>
  
  <p><code>$ git add -p</code></p>
  
  <p>The following appears:</p>
  
  <p><img src="https://i.stack.imgur.com/TNtNH.jpg" alt="enter image description here"></p>
  
  <p>I would love to split the hunk into smaller hunks, so I press <code>s</code>, and the following appears:</p>
  
  <p><img src="https://i.stack.imgur.com/QJV1S.jpg" alt="enter image description here"></p>
  
  <p>It seems that git didn't split it into smaller hunks. </p>
  
  <p>Let's try now to manually edit the current hunk. So I press: <code>e</code>.</p>
  
  <p>My default text editor (sublime text) opens up, and here I can edit the hunk:</p>
  
  <p><img src="https://i.stack.imgur.com/SCVeM.jpg" alt="enter image description here"></p>
  
  <p>I try to simply delete line 8,9,10,11 - because I want to stage only the <code>add</code> function.
  I save, close the editor, but git says:</p>
  
  <p><code>Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?</code></p>
  
  <p>I'm really new to <code>git add -p</code> and interactive staging, so maybe it's something I do wrong, or simply it's not possible what I want, and really hope some more experienced git users have some instructions maybe also a solution for me.</p>
  
  <p>Thank you!</p>