Git Stashing

“A big stash allows me to have a fluid sense of creativity - a looseness that is very much like playing. It opens me up, unlocks things. The creative bit takes all the other pieces - the possibility, the abundance, the connections, and the actual work of making yarn - bundles them, and explodes like a glitter bomb. It gets everywhere, it makes me smile, and a I can't escape it.” -- Clara Parkes

A Stash of One's Own: Knitters on Loving, Living with, and Letting Go of Yarn, https://www.goodreads.com/book/show/34227605-a-stash-of-one-s-own

This was done on Windows 10 via VS Code.

PowerShell and Git

Before going any further, I’d like to highlight a way to use Git within PowerShell on Windows. There is a section in the Git Book on this here: https://git-scm.com/book/en/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-PowerShell.

This has been a game changer for me as I need to see what branch I’m working on to prevent mistakes. More on posh-git can be found here: https://www.powershellgallery.com/packages/posh-git/1.0.0

How Git Stashing Works

According to the Git Book https://git-scm.com/book/en/v2 use git stash when

you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Of course, this requires that you know what the heck HEAD is. Basically, it is the top of a linked list--like structure that models a branch. See the following links:

Manual Stashing

Get the stash list:

git stash list

Pop the first stash off of the stack:

git stash pop

Apply changes from the stash but don’t pop the stack:

git stash apply

Save a stash with an annotation:

git stash save “saving Dockerfile"

The following will NOT work in PowerShell but they do work in git bash:

Show the changes for nth stash:

git stash show stash@{n}

Pop the nth stash:

git stash pop stash@{n}

Apply the nth stash (again don’t pop the stack):

git stash apply stash@{n}

And that’s how you do manual git stashing! What say we automate it with VS Code?

VS Code GitLens

Install either GitLens https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens or GitLess  https://marketplace.visualstudio.com/items?itemName=maattdd.gitless VS Code Extension. GitLens recently collaborated with GitKraken which in some people’s view makes it less friendly to open source.

GitKraken logo.

GitLess is a fork of GitLens minus the premium features of GitKraken. I did this with GitLess. Once installed you should be able to see “Source Control” inside the GitLess view. Below the Commits, File History, etc. should be a section called “Stashes.”

VS Code GitLess view. The GitLess extension is circled in green and the stashes are in the big pink circle.

Let’s go to the 2nd one back on the list, the one with hash e8a89b2

Applying the second stash back.

As you can see in the image you could also do things like compare against the current working tree or delete the stash entry from Git.

GitLens/GitLess has other cool stuff like Git blame, Remotes, etc.

Epiloge

Every so often it seems to me that I will never fully understand git — see, for example: Confusing git terminology (jvns.ca). This is a classic example of Impostor syndrome where in essence one feels like a pretender to a greater or lesser extent, and you have varying degrees of fear that you are only one mistake away from being unmasked. After reading How to Stop Sucking and Be Awesome Instead (codinghorror.com) I’m conceding that though I may never fully understand git’s stashing methodology, this article serves a step toward enlightenment.

Credits

“git stash save” is going away, but not anytime soon: https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning

git stash - Saving Changes | Atlassian Git Tutorial

https://dev.to/jamesqquick/supercharge-your-git-workflow-with-the-gitlens-vs-code-extension-4pci

https://www.keypuncher.net/blog/vscode-gitlens

https://www.atlassian.com/git/tutorials/saving-changes/git-stash

Difference between git stash pop and git stash apply - Stack Overflow

git stash apply version - Stack Overflow

How to view a git stash list (brainstormcreative.co.uk)

Previous
Previous

Git SSH Keys for Windows and WSL

Next
Next

Network Sockets in C++