You shouldn't git stash

You shouldn't git stash

Tags
Git
Published
September 9, 2019
Author
Mateusz Teteruk
Git stash. Do you use it on daily basis? Are you sure you’re doing efficiently? You shouldn’t git stash in every scenario. You can ask me why. From Android developer perspective, stashing is very easy in Android Studio IDE. You just click on VCS -> Git -> Stash changes, enter some name to distinguish that stash from dozen of others. And that’s it. You’re ready to go changing some code in existing branch or checkout another to fix critical bug which is already in production environment.
💡
Disclaimer. Importantly, there is case when I stash changes and it will be presented at the end of this post.

Advantages of git commit

First of all, commit is visible in git history without any fancy commands. It’s possible to see stashes in history but it’s tricky, we need to write more complicated commands.. We are lazy 😴 Of course you can add aliases (DRY!) but it does not solve the problem.
Built-in Git support in Android Studio is quite good – most of the times I use it to commit changes. In tab Version Control -> Log you can see git history but again, you won’t see there any stashes. That’s why it’s very easy to make a mistake.

Real life example

Okay, let’s say you are convinced to use it but „Matt, how to do it?”. Let me show you 😎
First, make normal commit with some informative message which will catch your eye in history like „wip”, „work in progress”, „working on FEATURE-123” etc.
git commit -m ‘work in progress’
Now you have your work stashed in very pleasant way. How to revert it? Git reset command is really handy.
git reset HEAD~1
You delete one commit before HEAD and apply changes to working copy. And that’s it. Really simple but powerful and helpful. If you come back to your „work in progress” branch you won’t forget about it which is possible in case of git stash.

When to git stash

Stashing is not always so terrible as you may think after reading my thoughts. I use stash if I need to do some quick changes, for example, there is critical bug you are working on. You find working solution which is ready to commit but you want to check another one that popped into your head. So what you can do? In this scenario, the simplest way is stash.
git stash // stash code git reset –hard // revert all changes git stash pop // add stashed code to working copy
Stash your changes and code new solution. If it’s better than previous one then commit it. If new solution is worse then remove it and apply last stashed changes to working copy. You’re working on same branch, it’s much quicker and you won’t forget about it. Consequently, stashing in similar use cases is recommended. You can read more about this command here: https://git-scm.com/docs/git-stash.

Summary

What do you think about it? How do you use git? Simple git push & git pull? Or maybe more? 🙂 What are your thoughts about presented topic?
Git commit and you’re ready to go! Happy coding!