git stash -p
If you only want to stash some of your changes, but not all of them, then git stash -p
is the tool for you.
Let’s take a look at my Unreliable Weather App as an example. This is the original code:
As an example, let’s say that I wanted to add another weather condition to it’s array, but I found myself adding some code to return the pollen count, and while doing that I saw an opportunity to refactor how the app retrieves a random number. But, wait! I totally derailed myself from my original goal, and I haven’t quite finished making my original changes. So, git stash -p
comes to the rescue!
I’m asked if I want to “stash this hunk” for each change I’ve made, and I decide to only keep the changes that update the conditions
array and the refactor for retrieving a random number.
And Voila! I’m done.
So far, the -p
flag has become my favorite flag to use in git commands.