Ever had a need to temporarily store changes that you made to your code? Without committing it? For example, you were in the middle of branch editing and someone asked you to collaborate on a different one? If you didn’t, you most likely will somewhere down the road in your developer career. At that point, it will be extremely useful to know how to use git stash. With the help of that command, you can temporarily store your current work to jump onto something else. Read on and find out how to Git stash changes.
How to Use Git Stash Command to Temporarily Save Your Changes
Okay, so picture this scenario. You are currently working on a piece of code that should bring a new feature to the application. All of a sudden, you urgently have to assist one of your co-workers with a bug that they found in a completely different branch. The first thing that you would want to do is to check which files you recently modified in the branch on which you are currently working. You can do that with a simple command.
This command will show you modified files. Below, you can find an example of the output.
Output:modified: app_layer.php modified: readme.txt modified: functions.php
Of course that you don’t want to lose your work on these files, but on the other hand you can’t just commit them. And that is where git stash shines!
The command itself is extremely easy and straightforward. An example is below.
The output will look similar to the one below.
Output:Saved working directory and index state WIP on master: 3tjaq12w Implement the new login box HEAD is now at 3tjaq12w Implement the new login box
And that’s it! All your work on the current branch is saved in some sort of clipboard. Feel free to start working on whatever popped up in the meantime.
Speaking of pop, here’s how you can return to the place where you left off once you are ready to continue. Simply type this into your terminal.
git stash pop
After the command above, you will be returned to your last saved state.
You’ll agree with us when we say that this is a very simple Git command to learn and it is of real value to having knowledge of it. We certainly hope that you’ll find a good use for what you learned today. Remember, developing is a never-ending course when it comes to learning.