Working with Git Stash
Quick memo showing how to work with git stash
Using Git Stash
The utility for git stash, in my case, comes from needing to change branches and having files not staged that I don’t want to commit yet.
So, my workflow in this case is to:
git stash push -m " write a descriptive message"
We can check all the staged instances as follows:
git stash list
stash@{0}: On branch-name: message 1
stash@{1}: On another-branch: message 2
Now that the directory is clean we can change branches, make the necessary changes there and return to our previous branch.
There, we can recover the state of our work being stashed. Let’s say that we want to recover the stash@{n}
.
git stash apply --index n
That’s it!. But wait, we should still have our stashed info when we list it. In order to remove it we can do
git stash drop stash@{0}
git stash list
stash@{1}: On another-branch: message 2
and we will see only the relevant stash data.
One way to do this two steps in one: apply + drop
is to use
git stash pop --index n