Writing code can be similar to writing tutorials. In either case, you’ll usually need to create and work through multiple drafts before arriving at the final version.
In an ideal configuration, you would write code for a feature, add that code to a staging area, and then commit it before moving on to the next section. This helps keep your commitment history clean. (Don’t worry if you don’t have a clean history. Many of us don’t.)
But now, imagine a scenario where you have multiple properties. You have committed before. You start the third one, but then you find out that you need to make the second one first, because the third one depends on it. It looks like you have to go back in time and build this second feature without the code changes in the third, and without deleting the code changes for the third.
So how do you do it?
In this article, you’re going to learn about GitStash, what it is, and the basic commands you need to be able to use it.
What we will cover:
To understand and get the most out of this tutorial, you’ll need a basic understanding of Git.
What is GitStash?
Git Stash provides storage (in the form of a stack) where you can store changes to your code. This allows you to keep these changes separate from the current working directory until you are ready to apply them.
git stash is a relatively simple command, and has some variations such as:
git stash pushgit stash popgit stash applygit stash dropgit stash cleargit stash showgit stash list
How to use GitStash
Pushing the code turns into a stash
To push ephemeral changes from the working directory to the stash, you can use git stash push. When you do this, the changes disappear from the working directory and are saved to the stash. The working directory is then reverted to the last commit (which is, for example, the feature below).
git stash push
You can also write git stash push Order as only git stash – It means the same thing and does the same thing. You can also add a message to it -m or --message flag:
git stash push -m "Feature two I was working on"
you can use -u flag or --include-untracked Adding untracked changes to the stash. That way, changes not yet tracked by git can be added to the stash.
git stash push -u
git stash push --include-untracked
On the other hand, you may decide to push only staged changes to the stash with it -s or --staged flag:
git stash push -s
You can also suppress feedback messages -q or --quiet flag:
git stash push -q
When you work with your edits in the current working directory, you can commit and commit without older changes bleeding into it. They are safely tucked away in the stash.
Whenever you use the git stash command, you add a stash to the stash list. Stations are identified by their index numbers.
Applying changes from the stash to the working directory
Let’s say you had to settle immediately. You’ve committed, and you want to continue with what you were working on. You can restore changes from the stash to continue working on them.
You can do this using git stash apply command or git stash pop Order:
git stash apply
git stash apply Applies the latest changes from the stash to the working directory, but does not delete the code from the stash. You can select a particular entry to apply for by index number:
git stash apply --index stash@{1}
git stash popon the other hand, applies the latest changes from the stash, then deletes them from the stash. Basically, popping off a stack. You can select a particular stash entry to pop by index number.
git stash pop
git stash pop --index stash@{1}
Listing items in a stash list
you can use git stash list For a list of stashes in the stash list. It is organized by index number (such as {0}, {1}, and so on). Whenever you push a git stash, it adds a stash to the stash list.
git stash list
stash@{0}: WIP on master: b2a2709 Feature one
stash@{1}: WIP on master: b2a2709 Feature one
Removing items from the stash
you can use git stash clear To clear the stash list. But if you just want to skip a particular entry from the stash list, you can use git stash drop And specify the entry you want to skip by index number. It does not apply its changes to the working directory.
git stash clear
git stash drop stash@{0}
Creating a new branch from stash
You can also create a new branch from stash using git stash branch. There is syntax git stash branch
git stash branch premium-branch stash@{0}
If you don’t add a stash index, it will only use the last stash.
git stash branch premium-branch
Showing changes in stash
you can use git stash show To show the changes you have made to your changes.
C:\file-path\git_stash_example> git stash show
text.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
The result
GitStash is one of those quiet tools that becomes indispensable when your workflow starts to get messy. This allows you to harbor incomplete views, switch contexts without panic, and keep your commits clean. With it, you can safely explore hotfixes and dependent features without messing up your commit history.
If you enjoyed this article, share it with others. You can also reach me LinkedIn or X.