We’ve all been there. Whether you were multitasking or just simply selected the wrong working branch, there has likely been a time that you wanted to save your file changes but you weren’t quite ready to commit them.
Enter stashing. Stashing is a Git function that allows you to temporarily save your file changes without applying them to your project. This can be especially helpful when collaborating with others, giving you more time to perfect your changes before sharing.
What is Stashing?
In this article—and in the latest Git tutorial video in our Learning Git with GitKraken series—we’ll answer the question ‘What is stashing?,’ and show you how to use the GitKraken Git Client to quickly and easily stash your changes.
Watch this beginner Git tutorial video to see how easy it is to stash file changes in GitKraken!
Get the latest version of the GitKraken Git Client:
Managing Distractions and Mistakes
In most cases, when you edit, add, or delete a file in your working repository, you would stage and then commit those changes to apply them to your project.
But let’s say, for instance, that your team lead asks you to review another branch…like Right. Now. Your attention is diverted and you must switch branches (hurry!!), but you’re not ready to commit your changes (ah! panic sets in…).
Or, worse yet, you’ve been working on the wrong branch entirely (Son of a!). You don’t want to lose your work, but you can’t apply the changes to the branch currently checked out (cue sobbing at your desk).
These are two common developer situations where stashing can save the day. When you create a stash, you are saving uncommitted changes so that you can work on other things without losing your changes.
Stashing: Apply vs Pop
When you’re ready to finalize these saved changes, you have two options: apply or pop.
Apply will take the stashed changes, apply them to your working directory, and keep the changes saved as a stash.
Pop will do the exact same thing for the first two steps, but it will permanently delete the stash.
A Word of Caution
While stashing can play a helpful role in temporarily saving files for later access, it is not a substitute for committing changes.
Our team also suggests, as a best practice, to use stashing in moderation. If you are constantly utilizing this feature, you will likely lose track of which saved changes are where, making your project increasingly chaotic.
Stashing in GitKraken
GitKraken makes stashing file changes extremely easy and clear, helping you avoid mistakes and ensuring you’re committing files to the appropriate branch.
Let’s take the following example: You’ve checked out a feature branch and are making changes to multiple files. Gulp… You just realized you checked out the wrong branch.
To give your future self some context, we suggest naming the stash, which can be done easily by simply typing in the text field next to the WIP node at the top of your commit graph.
After naming your stash, click the Stash button on the top toolbar inside of GitKraken. Your stashed changes will now appear at the top of your graph as WIP on [insert branch name]: [insert stash name].
Let’s say we want to pop this stash. Double-click to check out the correct branch where you wish to see your changes reflected, and click the Pop icon in the GitKraken toolbar. This will apply the changes you stashed onto the correct branch and then delete your stash.
From here, you can stage and commit your changes.
Alternatively, if you wish to apply the stash, you can right-click directly on the stash to access this option in GitKraken. Performing this action will apply the stashed changes onto the correct branch, but will also keep the stash intact.
When done correctly, the stash function can be a real lifesaver. And thankfully, the GitKraken Git Client makes it intuitive and clean so you can keep your project organized to more effectively collaborate with your team.
Stay on the cutting edge of software development by getting innovative tips, trends and stories delivered to your inbox every month!