Getting Git: Stash Some the Things
I am a consummate git stash
addict and use it daily when:
- Switching branches
- Squirreling away some changes for another branch/commit
- Hiding some for-testing-purposes-only code between different builds
I am also not a git expert by any measure.
Often (too often) I have wondered how I could stash only some of the changes in my working directory. Recently, I actually RTFM and discovered a couple of ways. Woohoo!
git stash --keep-index
git stash --patch
As usual, you are now free to go, dear reader. For a little more blather, travel forth.
git stash --keep-index
Most frequently I use git stash --keep-index
to exclude changes in the index/staging area from the stash.
First, I add the still-working-on-them changes to the index:
Second, I execute git stash --keep-index
:
And that's it! My for-later changes are stashed away and I can keep working.
If I was cheating and only using the index as a filter for git stash
, I could move those changes back via one of two ways:
git reset HEAD --
git rm --cached -r
.
git stash --patch
Alternatively, the --patch
option provides an interactive review of all of the working directory changes and the ability to selectively add hunks to the stash.
While I would absolutely suggest trying out git stash --patch
, I personally do not use it often: it just does not jive as well with my workflow.
git commit -m "Wrap up."
I feel the need to acknowledge that some folks don't like using git stash
period and that those folks argue that because git makes committing and branching so easy, git stash
is unnecessary and potentially dangerous. In regards to this conversation, I also acknowledge that I could easily move for-later changes to a branch.
Respectfully acknowledged.
I will keep on stashing.