0
0
Gitdevops~5 mins

git stash to save changes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you start working on changes but need to switch tasks quickly without committing unfinished work. Git stash lets you save your current changes safely and come back to them later.
When you want to switch branches but have uncommitted changes that you don't want to commit yet
When you need to pull updates from a remote repository but your local changes would cause conflicts
When you want to try a quick fix or experiment without losing your current work
When you want to clean your working directory temporarily without losing your progress
When you want to save multiple sets of changes separately and apply them later
Commands
This command saves your current changes (both staged and unstaged) to a new stash and cleans your working directory so you can work on something else.
Terminal
git stash
Expected OutputExpected
Saved working directory and index state WIP on main: abc1234 Initial commit
Shows all the stashes you have saved so far, so you can see what changes are stored.
Terminal
git stash list
Expected OutputExpected
stash@{0}: WIP on main: abc1234 Initial commit
Restores the most recent stash changes back into your working directory without removing the stash from the list.
Terminal
git stash apply
Expected OutputExpected
No output (command runs silently)
Deletes the most recent stash from the stash list after you no longer need it.
Terminal
git stash drop
Expected OutputExpected
Dropped stash@{0} (WIP on main: abc1234 Initial commit)
Key Concept

If you remember nothing else, remember: git stash saves your unfinished work safely so you can switch tasks without losing progress.

Common Mistakes
Running git stash but forgetting to apply or pop the stash later
Your saved changes stay hidden and you might lose track of them or think they are gone.
Always run git stash apply or git stash pop to restore your changes when ready.
Using git stash without committing important changes first
If you accidentally drop or clear stashes, you lose work that was never committed.
Commit important changes before stashing to avoid accidental data loss.
Assuming git stash saves untracked files by default
Untracked files are not saved unless you use special flags, so they remain in your working directory.
Use git stash -u to include untracked files if needed.
Summary
git stash saves your current changes and cleans your working directory.
git stash list shows all saved stashes.
git stash apply restores changes without deleting the stash.
git stash drop deletes a stash when you no longer need it.