Stashing only unstaged changes in Git
Published on in Git
git stash -k
doesn't work as expected.
Use a temporary commit instead.
Table of contents
Non-solution
git stash -k # -k = --keep-index
What this does:
- Keeps the staged changes: nice.
- Stashes unstaged and staged changes: not nice since the stash entry will also contain the staged changes. This can be problematic as (in this blog post) we want to stash only unstaged changes.
Solution
Using my Git aliases:
git c -m wip
to create a temporary commitgit stash
to stash the remaining (unstaged/uncommitted) changes- Optionally use
-u
(=--include-untracked
) to also stash untracked files
- Optionally use
git undo
to undo the previous commit (the temporary commit done in step 1)- Optionally use
--soft
to keep the changes in the staging area
- Optionally use
If not using my Git aliases:
git c
=git commit
git undo
=git reset HEAD~
Source of the solution: Stephen Hanson's answer on Stack Overflow.
My point is that my Git aliases make the solution even nicer,
especially git undo
since I can't remember the underlying command,
plus git reset
is scary (but the alias makes it less scary).