In my introductory post on git, I told you that git uses three areas to track changes. Well, maybe I lied to you in this case. There are actually four areas available and I will cover this fourth area in todays post.
The fourth area is called Stash. Basically, it works like a clipboard to which you can save current changes.
Concepts of the Stash …
Imagine the following scenario: You are working on a change. In the middle of your work you remember that you also wanted to do something else. This other change is important and should be handled immediately. However, it cannot be implemented with the uncomitted changes you are currently working on. In short, you need the status of the last commit, but you don’t want to discard your current changes and you also can’t commit them yet.
This is where the stash comes in handy. The stash basically allows you to copy the changes that are located in your working area and index and stores them. It also checks out the latest commit so your working area and your index a re in a clean state. You can have multiple of such clipboard contents on your machine and you can restore each of them individually.
… and how to apply them
All of the stash commands are initiated by git stash. To stash the current changes, use git stash save. For this, you can also simply use git stash without any suffixes. If you now use git status to view your changes, you’ll see that there are none available and your working area and index are in a clean state.
You can then use git stash list to view all the contents of the stash. You’ll notice that each entry has an id, which can be used to restore an individual entry. Per default, the most recent entry will be used.
To restore an entry from the stash, use git stash apply . This will restore the changes to your working area and index, but the entry will also remain in the stash. To clear all entries from the stash, use git stash clear.