For all of those completionists out there who don’t like to lose anything, the thought of any kind of data loss—no matter how minor—is a threat.
As you may already know, Git lets you stash (set aside) changes to your files without requiring you to commit. In this article, we’ll show you how to pop
the contents of a Git stash while preserving those changes in the stash.
- How do I apply a stash without losing it?
- What happens during a normal stash pop?
- Stashes are not for long term storage
Git is a popular version control system for anyone managing files on private cloud servers or local file structure.
Cherry-picking From the Stash List (With Git Stash Apply)
For more information on using the Git stash list, and how to store changes there, see our full guide on how to Git stash.
The stash list lets you put current changes aside so you may, as needed, switch between branches.
For one reason or another, you may have to switch branches in the course of your work. Without having to rush your changes and commit, the stash lets you set work aside and resume it later with git stash pop <stash#>
.
But what if you want to set changes aside and just let them stay there? Let’s say you want to save a current state in its stash and keep working while preserving the state of the stash as is? Maybe you want to pull that stash in a different context.
In this latter instance, you would use apply
instead of the pop
command.
For example, if you want to apply
the contents of stash 1, you would run this command:
git stash apply 1
This command will restore the contents of the stash at reference 1 without dropping the stash itself.
The Normal Stash Pop
During normal operation, a git stash pop
(plus the optional number of the stash item) will restore the state of that stash and then drop that stash entirely.
Example output from a restored/dropped stash:
Dropped refs/stash@{1} (9fe937e49d99e9ee2d1d2705a27690211f403681)
This where the use of apply
instead of pop
saves the stash you popped from getting dropped.
Warning Against Stashing Items Long Term
The stash is best used as a temporary storage space to clear your working directory.
The apply
command exists if, for any reason, you might want to apply a piece of content to a different branch or over another commit.
But in general, the stash should not be relied upon as a permanent storage space.
To freeze your project files in one state long term, it is best to do a proper commit.
If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.
CentOS, Debian, or Ubuntu No Bloatware SSH and Root Access