Stash as a Commit
What is a Stash? Link to heading
From Julia Evans’ blog article titled Some miscellaneous git facts
👉🏽 **the stash is a bunch of commits**
When I run
git stash
to stash my changes, I’ve always been a bit confused about where those changes actually went. It turns out that when you rungit stash
, git makes some commits with your changes and labels them with a reference calledstash
(in.git/refs/stash
).Let’s stash this blog post and look at the log of the
stash
reference:$ git log stash --oneline 6cb983fe (refs/stash) WIP on main: c6ee55ed wip 2ff2c273 index on main: c6ee55ed wip ... some more stuff
Now we can look at the commit
2ff2c273
to see what it contains:$ git show 2ff2c273 --stat commit 2ff2c273357c94a0087104f776a8dd28ee467769 Author: Julia Evans <julia@jvns.ca> Date: Fri Oct 20 14:49:20 2023 -0400 index on main: c6ee55ed wip content/post/2023-10-20-some-miscellaneous-git-facts.markdown | 40 ++++++++++++++++++++++++++++++++++++++++
Unsurprisingly, it contains this blog post. Makes sense!
git stash
actually creates 2 separate commits: one for the index, and one for your changes that you haven’t staged yet. I found this kind of heartening because I’ve been working on a tool to snapshot and restore the state of a git repository (that I may or may not ever release) and I came up with a very similar design, so that made me feel better about my choices.Apparently older commits in the stash are stored in the reflog.
The Rationale Behind Preferring Commit Over Stash Link to heading
For people accustomed to the command-line interface of Git, the concept of committing instead of stashing may seem unconventional. However, when your Git workflow is integrated into Integrated Development Environment (IDE), particularly Visual Studio Code, this approach becomes beneficial.
The User Interface of Atom Link to heading
The user interface of Atom was great, particularly its tri-pane design that always keeps the File Explorer, Code, and Git in view.
The Atom UI with File Explorer, Code Area and Git/Github Panel
The most useful part of this is that it continually updates a list of changes made to files on the right side of the screen, providing a general overview of the magnitude of your modifications. This visibility encourages experimental changes, which can be easily cherry-picked or discarded.
Atom Git Pane
The Workflow in Atom Link to heading
Consider a scenario where you are working on feature-branch-1
and need to switch to the master
branch. Typically, you would stash your changes in feature-branch-1
. However, Atom provides an alternative commit method.
Visual Studio Code: A Comparison Link to heading
By default, Visual Studio Code does not offer the same layout as Atom. However, this is arguably advantageous, and a similar workflow to Atom can be achieved in Visual Studio Code. This can be done by enabling a Secondary Side Bar and moving your “Source Control” and “Commits” to the second pane.
A lot more cluttered than my Atom. But it also does a lot more than Atom!
The Workflow in Visual Studio Code Link to heading
Additional: Command Line Approach Link to heading
The Merits of Stash Commit Link to heading
The primary advantage of a stash commit is that it is local to a branch. This is particularly useful when working on multiple features and needing to stash changes to switch branches (often to master to create a new branch). Upon returning, the stash commit is visible, allowing you to resume where you left off. This is especially helpful when revisiting a branch after a significant period, as remembering that you stashed some code may not be evident.
The Argument for Stash Link to heading
git stash
effectively clears your branch of changes for later use, which is necessary if you do not have a branch to perform a stash commit.- If you have a piece of code that is frequently needed but cannot be committed,
git stash
is the preferred method.
References Link to heading
Julia Evans’ Blog Article: https://jvns.ca/blog/2023/10/20/some-miscellaneous-git-facts/