Git - How to edit old (not previous) commit with some of the unstaged changes from current index (current state)?
git
Solution
Your plan sounds good. A `git stash apply` or `git stash pop` will modify the working tree and/or index, but will not change HEAD, so you should be able to do it while in a rebase edit.
Problem
I've taken a look at these previous questions already: - Howto add a changed file to an older (not last) commit in Git - How do I edit a previous git commit? They don't exactly address a particular issue though - there are other changes in the index! When running the `rebase` command, git complains: `Cannot rebase: You have unstaged changes.` Scenario: The commit previous to the last one (do I refer to that as "2 HEADs ago"?) 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. I'm imagining the way to do this would be to: - `stash` all of my current changes - `rebase -i` to the previous to last commit (changing index and moving Head, right?) - load the stash into my index without changing Head (how?) - use `add -p` and `commit --amend` to selectively modify this old commit - `rebase --continue` to finish (updates children, moves Head back to where I started, what happens to index?) - then pop/clear the stash (index back to where I started). Is this correct? If it is, how do I do step 3? If it isn't, what should I be doing instead? 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. Solution: For anyone else this may help, these are the steps I actually took: - `git stash` all of my current changes - `git rebase -i <ID>` to the parent of the previous to last commit, changing index and moving Head - `git stash apply` load the stash into my index without changing Head - if you have conflicts, `git reset HEAD <file>` to unload files staging. Make sure staging is clear. - use `add -p` and `commit --amend` to selectively stage changes and commit them - `git reset --hard` to discard index so it matches Head - `git rebase --continue` to finish. updates children, moves Head back to very start, but with changes - history is now forked into two versions. The other branch ends at the WIP previously stashed - then pop the stash to bring index back to where I started. The other branch is also cleared.