How to Use Git Stash: A Step-by-Step Guide

Resumo

Pixel shares a step-by-step guide to Git stash, showing how to check changes with git status, save work using git stash push -m, and review saved entries. Tested with Git 2.43, the workflow helps developers protect unfinished changes and switch tasks without losing work.

DEV Community

How to Use Git Stash Step by Step

Originally published on CodingCush. Git stash is a temporary shelf for your uncommitted work. Run git stash push and Git stores your changed files safely, then cleans your working folder so you can switch branches or fix something urgent. Later, git stash pop takes the work off the shelf and puts it back. This guide walks through the whole cycle step by step, using a tiny project tested with Git 2.43. Step 1: Check Your Work with git status Start with a small project. Create two files, commit them, then change one line in homepage.txt so there is real unfinished work to stash: git init -b main
git add homepage.txt notes.txt
git commit -m "Add homepage and notes"
git status --short
Our session began with homepage.txt containing Welcome to my site, then we changed it to Welcome to my site - new hero draft. The status check printed: M homepage.txt
The letter M means modified. Always check status before stashing: later the same command proves the shelf worked, because it will report a clean tree. If branches are still new, our guide to Git branches explained, including how to create, switch, merge and delete branches, covers the background for Step 4. Step 2: Save the Work with git stash push -m Now shelve the change and give the stash a clear message: git stash push -m "WIP hero text"
Expected output, exactly as printed in our test: Saved working directory and index state On main: WIP hero text
Git saved the modified file as a stash entry, restored it to the last committed version, and kept your message so future you can tell stashes apart. Always use -m. A shelf of unnamed boxes becomes a small mystery you created for yourself. The official git stash documentation describes push as saving local modifications and reverting the working directory to match the last commit. Step 3: See What Is on the Shelf with git stash list git stash list
git status
Our list printed: stash@{0}: On main: WIP hero text
And git status reported nothing to commit, working tree clean. The newest stash is always stash@{0}; older entries move down to stash@{1} and stash@{2}. That number matters later, because apply and drop can target one entry. Terminal window showing the real Git stash session tested for this article: the push that saved the hero draft, the stash list, and the pop that restored it. Step 4: Switch Branches and Do the Urgent Job This is the everyday reason stash exists. You are halfway through the hero text when an urgent fix lands. Your work is shelved and the tree is clean, so switching is safe: git checkout -b urgent-fix
git commit -am "Add urgent fix"
git checkout main
In our session we created urgent-fix, added a small fix.txt and committed it there as [urgent-fix 1bddeb5] Add urgent fix, then switched back. Without the stash, Git might have carried your half finished edit into the other branch or refused to switch. With it, each job stays in its own place, and nobody needs a messy commit called "temp do not look". Step 5: Bring the Work Back with git stash pop Back on main, take the top entry off the shelf and apply it in one move: git stash pop
cat homepage.txt
git stash list
The pop restored homepage.txt and listed it as modified: homepage.txt. It ended with a line like Dropped refs/stash@{0} plus a long hash, confirming the entry was removed. The file check printed Welcome to my site - new hero draft, so the draft is back word for word. The final list printed nothing: the shelf is empty. We then committed it with git commit -am "Save hero draft". pop vs apply: Keep the Copy or Not? git stash apply restores the work but keeps the entry. Use it to test the changes on another branch too, or to keep a safety copy. In our second test we stashed a Second draft, ran apply, and the list still showed stash@{0}: On main: Second draft. Then git stash drop removed it, printing Dropped refs/stash@{0} with its hash. The chapter on stashing and cleaning in the Pro Git book shows the same flow on a larger project. For daily reference...
How to Use Git Stash: A Step-by-Step Guide
Powered by Readybot.io

As últimas novidades de Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …