This is the only answer that tells you to use git stash -k
, which you will need...
if you already spent an hour with git add -p
and then decided you want to test what you added to the index before doing the actual commit. In that case do not use plain git stash
!
Instead do:
git stash -k
That keeps the index and removes the rest that is still in the working directory and wasn't added to the index yet. Exactly what you want.
Now you can try to compile/test and commit. I.e.
make
git commit -m 'Yay!'
Then get back the uncommitted changes with
git stash pop
If you discover that it does NOT compile however, then making changes and adding those also the index and committing that might confuse git stash pop
. It isn't that good when it comes to merging. In that case you probably should just commit anyway; thus:
make
git commit -m 'Grrrr'
Then create a new branch,
git switch -c tmpbranch
do your work there (changing code, doing testing and more commits)
/* blood sweat and tears */
Once everything works commit it to the new branch
commit -a -m 'Finally!'
go back to the old branch and then do the git stash pop
with the same working directory as where you was when you pushed to the stash.
git checkout youknowwhatbranchyouwereonright
git stash pop
Commit that too, otherwise you can't merge the tmpbranch.
Then merge the temporary branch that you created.
git commit -a -m 'Still working on this.'
git merge tmpbranch
/* fix collisions and commit */
Now you can do a rebase to put the 'Still working on this' at the top and squash/fixup the rest into a single comment. For example
git rebase -i
might give you:
pick 540623a Grrr
pick a8589d3 Still working on this.
pick d3b602c Finally
Then change that to:
reword 540623a Grrr
fixup d3b602c Finally
pick a8589d3 Still working on this.
And finally undo the last commit (the 'Still working on this')
git reset HEAD~1