this happens quite often to me:
After a day of coding i start to stage relevant changes for a feature. I do this mostly by "git add -p" to selectively add only relevant changes (keep debug things and so on out).
But then i realize that i also did something which is also worth to be committed. Its not related to the feature, but affects same code. Therefore i want to commit this before. Unfortunately i already spend a lot of time for staging the feature. How do you handle this situation?
My current solution is this:
(Imagine parts of feature "B" are already staged, now you realize there is feature "A")
cp .git/index .git/index_bak #Save partly staged feature B
git reset #Start again
git add -p #Stage only A
git commit #Commit only A
mv .git/index_bak .git/index #Restore stage. This is now a bit strange
#as it seems to remove feature A. Therefore
#reapply feature A (which is still in the
#working directory) again - only to the stage
git diff -U0 HEAD~1.. | git apply --cached --unidiff-zero
git add -p #Continue stageing B