Git stash is a built-in command with the distributed Version control tool in Git that locally stores all the most recent changes in a workspace and resets the state of the workspace to the prior commit state.
A user can retrieve all files put into the stash with the git stash pop and git stash apply commands. Git stash acts as a mechanism to locally version files without those versions being seen by other developers who share the same git repository.
Git stash vs. commit
The git commit and git stash commands are similar in that both take a snapshot of modified files in the git working tree and store that snapshot for future reference. The key differences between the two are as follows:
- A commit is part of the public git history; a stash is stored locally.
- A commit creates a new save point on a branch; a stash reverts to a previous save point.
- A new commit leaves files in the working tree unchanged; a stash resets files in the working tree to the previous commit point.
- A commit is a public record of file changes; a stash is local.
Git stash vs. reset
The git stash and the git reset hard commands are similar, as both commands will revert all files in the working directory back to the state at the point of the previous commit. Differences between the two include:
- A reset creates a new commit point in the branch history; stash does not.
- A reset can jump back to any prior commit; a stash can only reset the files in the workspace to the point of the previous commit.
- A hard reset will discard all changes; a stash saves a snapshot of all locally modified files.
Git stash vs. stage
The git stash and git stage commands can be confused because of their similar names, but the two commands are different. The git stage command simply adds files to the git index. This allows those files to be part of a filesystem snapshot when a git commit occurs. This is a different construct as compared to git stash.
Popular git stash options
The most common git stash options include:
- git stash push: Creates a new stash and rolls back the state of all modified files;
- git stash pop: Takes the files in a stash, places them back into the development workspace and deletes the stash from history;
- git stash apply: Takes the files in a stash and places them back into the development workspace, but does not delete the stash from history;
- git stash list: Displays the stash history in chronological order; and
- git stash clear: Removes all entries in the git stash history.
The git stash save command is deprecated in favor of git push.
Which folder stores git stash history?
The latest git stash is stored in the hidden folder .git/refs/stash. All other stashes are found in the reflog of this folder.
Git stash best practices
The git stash command is a utility that allows a developer to temporarily save modified files. However, it is a utility that should only be used occasionally because it only involves local repository history. Code stored in a local stash is not visible to other developers, nor is it replicated to other git repositories like GitHub or GitLab when a push occurs.
It's a better option to create development branches and commit regularly to these branches rather than a stash. These branches will replicate to other team members after a push and survive any local file system failures. Use of the stash makes the most sense when the changed files are shelved only temporarily and the intention is to come back within a short timeline.