DEV Community

Cover image for Some common scenarios where using git stash can be useful in a development workflow.
raielly
raielly

Posted on • Updated on

Some common scenarios where using git stash can be useful in a development workflow.

Git stash can be a useful tool in certain situations, such as when you need to quickly switch to another branch or work on a different feature. However, it's important to use this feature accordingly and in conjunction with other Git commands and techniques to ensure that your development workflow stays organized and manageable.

I find Git stash particularly useful in situations where I realize that I forgot something in my previous commit, and have already moved on to working on the next one within the same branch.

Here are some commands you can use:

git stash (command to save any changes that you haven't committed yet into a "stash". Please note that this command removes those changes from the working tree.)

git checkout other_branch (to switch to the desired branch -- in this case 'other_branch')

git stash list (check list of stashes)

git stash apply (apply the saved changes from stash to the current branch's working tree.)

git stash apply stash@{12} (in case you have multiple stashes, you can specify which stash to apply. Here, we are applying the 12th stash.)

git stash drop stash@{0} (to remove from stash list -- in this case stash 0)

git stash pop stash@{1} (to apply selected stash and drop it from stash list)

PS.
To save git stash with custom commented, we simply use
git stash push -m "my-stash-name" and then to apply changes use
git stash apply stash^{/my-stash-name}

So basically, Stash command is like a little magic trick for your changes. It lets you keep some changes that you don't need or want right now, but you may need them later on.

Top comments (2)

Collapse
 
kenneth_sagarino_45baa0b9 profile image
Kenneth Sagarino • Edited

What ?? You've got to be kidding me ? Been searching for weeks about something like this luckily I got you.

Collapse
 
raielly profile image
raielly

@kenneth_sagarino_45baa0b9 I'm glad I could help! You've discovered a fantastic blog! 🔥