title: Git - How to edit old (not previous) commit with some of the unstaged changes from current index (current state)?
body: <p>I've taken a look at these previous questions already:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/2719579/howto-add-a-changed-file-to-an-older-not-last-commit-in-git">Howto add a changed file to an older (not last) commit in Git</a></li>
  <li><a href="https://stackoverflow.com/questions/9103396/how-do-i-edit-a-previous-git-commit">How do I edit a previous git commit?</a></li>
  </ul>
  <p>They don't exactly address a particular issue though - there are other changes in the index! When running the <code>rebase</code> command, git complains: <code>Cannot rebase: You have unstaged changes.</code></p>
  <h3>Scenario:</h3>
  <p>The commit previous to the last one (do I refer to that as &quot;2 HEADs ago&quot;?) was a refactor commit. I currently have in the index many unstaged changes, but only some of which I want to add to the previous to last commit.</p>
  <p>I'm imagining the way to do this would be to:</p>
  <ol>
  <li><code>stash</code> all of my current changes</li>
  <li><code>rebase -i</code> to the previous to last commit (changing index and moving Head, right?)</li>
  <li>load the stash into my index without changing Head (how?)</li>
  <li>use <code>add -p</code> and <code>commit --amend</code> to selectively modify this old commit</li>
  <li><code>rebase --continue</code> to finish (updates children, moves Head back to where I started, what happens to index?)</li>
  <li>then pop/clear the stash (index back to where I started).</li>
  </ol>
  <p>Is this correct? If it is, how do I do step 3? If it isn't, what should I be doing instead?</p>
  <p>Also, note that I'm still learning about git and am still not 100% sure I'm referencing things in git (Head, index, stash, etc) properly.</p>
  <hr />
  <h3>Solution:</h3>
  <p>For anyone else this may help, these are the steps I actually took:</p>
  <ol>
  <li><code>git stash</code> all of my current changes</li>
  <li><code>git rebase -i &lt;ID&gt;</code> to the <em>parent</em> of the previous to last commit, changing index and moving Head</li>
  <li><code>git stash apply</code> load the stash into my index without changing Head
  <ul>
  <li>if you have conflicts, <code>git reset HEAD &lt;file&gt;</code> to unload files staging. Make sure staging is clear.</li>
  </ul>
  </li>
  <li>use <code>add -p</code> and <code>commit --amend</code> to selectively stage changes and commit them</li>
  <li><code>git reset --hard</code> to discard index so it matches Head</li>
  <li><code>git rebase --continue</code> to finish. updates children, moves Head back to very start, but with changes
  <ul>
  <li>history is now forked into two versions. The other branch ends at the WIP previously stashed</li>
  </ul>
  </li>
  <li>then pop the stash to bring index back to where I started. The other branch is also cleared.</li>
  </ol>