>Git stash: Managing Uncommitted Changes Effectively.
Something happened recently while I was working on a certain team project. Operating from my forked repository, I had to add a couple of features, A PR for a small but important feature was made and promptly merged, I had to update this repo from upstream before a final commit, so I followed the steps... fetch upstream, checked out main, and merged upstream main, I pushed the merged changes to the main of the fork. However, the result was not what I wanted as I had made an oversight of one crucial step. Updates had been made upstream, so when I fetched, these changes came along, and the merge gave me an error flag;
“Your local changes to the following files would be overwritten by merge: {filename}, please commit your changes or stash them before you merge...”
This has probably happened to many, or maybe not in exactly this same manner but sometimes when working on a project, you may find yourself in the middle of more than one task. Imagine being halfway through implementing a new feature, when suddenly you need to fix a bug, that may affect your progress. How do you handle these interruptions without losing track of your work? I love the fact that Git will always run ahead and see any potential harm to the project code before any changes are implemented.
In hindsight, checking the git status
before fetching and merging would have ensured that I noted any uncommitted changes. But this happens to the best of us. It was this encounter that prompted me to share how commands like git stash
, git stash apply
, and git stash pop
, - three powerful Git commands - can be used to manage changes effectively.
Needing to switch quickly switch contexts is common in development. When this happens and you don’t want to commit just yet, and you also don’t want to lose your current progress, this is where git stash
comes in handy.
git stash
: Putting your work on hold.
‘git stash’ allows you to temporarily save your uncommitted changes and revert your working directory to a clean slate. Think of this as a pause button for your current work. When you run git stash
, Git takes your current file modifications and stores (stashes) them away making it easy for you to switch to a different task.
git stash
By doing this, the working directory is cleaned up, and you are free to look at that bug fix, or whatever task you need to switch to.
git stash pop
: Resuming where you left off.
Once you are done with the bug fix, (Be sure to have committed those changes before coming back) and would like to switch back to where you were, you can now return to your previous task. To do this, you use the ‘git stash pop’ command, which then retrieves the most recently stashed changes, and applies them back to the working directory.
git stash pop
This command does not only apply the changes, but it also removes them from the stash list, this effectively un-pauses your work, allowing you to continue from where you left off.
git stash apply
: Reusing stashed changes
But wait, if the git pop empties the stash list, what happens if you want to apply the stashed changes without removing the stash? This is where we use ‘git stash apply’, It allows the reapplication of stashed changes multiple times and even on different branches without losing the original stash. Consider the following scenarios where you might want to keep your stack intact:
You are working on a feature and stash your changes; you then realize that these changes are also relevant to another branch.
You want to apply stashed changes to first test something in your working directory. If everything works as expected, you might later decide to pop the stash, but if not, your stash is still safely stored.
If you anticipate potential merge conflicts when applying the stashed changes, using
git stash apply
allows you to manually resolve any issues without losing the original stash.
git stash apply
To put it in more context, here is a scenario...
You are working on feature A and have made several changes.
You receive a request to first fix a bug, Bug B.
You stash your changes with git stash.
You switch to the bugfix branch and deal with bug B.
You commit the bug fix.
You return to your feature branch and use git stash apply, because you just realized the same changes are what you’ll need in another feature.
You switch to that other feature and use git stash apply, again to apply the same stash.
Finally, when you are sure you no longer need the changes, you pop the stash.
Using ‘git stash apply’, gives you more flexibility in managing your changes, while making sure that you can reuse the stashed changes across different contexts.
Note that once you have used ‘git stash apply’, you must commit your changes as ‘git stash pop’ will reapply the stash again if you haven’t made any commits since applying it. To empty your stash when you no longer need it, you can either pop or ‘drop
’ the stash.
If you have changes in your most recent commit that you would like to compare with the stashed changes, you can use the git stash show
command to see the differences.
git stash show
By using git stash
, git stash apply
, and git stash pop
, you can manage changes effectively while switching between tasks or branches without losing your work. By thinking of your changes as a stack, you are sure to better manage and appreciate how these commands help with juggling tasks seamlessly. Remember, ‘git stash’ is your pause button, ‘git stash pop’ is your resume button, and ‘git stash apply’ is your reapplication.
Well friends, feel free to share your own related stories and additional tips in the comments section, and remember to subscribe to my FREE newsletter and get notified on future content. Until next time!
Subscribe to my newsletter
Read articles from Mandla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mandla
Mandla
Junior Developer and Tech Enthusiast with a Drive to Transform Industries through Innovation.