Git Stash

Divya Srinivasan

Key Commands for Stashing

git stash – Save your current changes
git stash push -m "message" – Stash with a descriptive message
git stash list – View all saved stashes
git stash branch <branchname> – Create a new branch and apply a stash to it

What is Git Stash? Why Use It?

When you’re working on something but suddenly need to:
  • Switch to another branch
  • Fix a critical bug
  • Experiment without affecting your current progress
…you might not be ready to commit incomplete work.
git stash helps by temporarily saving (stashing) your uncommitted changes, so you can work with a clean directory. Later, you can restore those changes and continue right where you left off.

Common Use Cases

  • Switch branches safely: Stash your work before switching to avoid conflicts.
  • Handle urgent fixes: Quickly stash your work to address something critical, then return.
  • Keep work-in-progress separate: Avoid committing unfinished or messy changes just to switch context.

Stash Your Changes

To save your current modifications (both staged and unstaged tracked files), simply run:
git stash
This clears your working directory while keeping your changes safe, so you can restore them when you’re ready.

Example: 

Stash Your Work
git stash
Saved working directory and index state WIP on main: 1234567 Add new feature
This command temporarily stores your uncommitted changes and resets your working directory to match the last commit.

Stash Changes with a Custom Message

When you stash your work, it’s helpful to add a message describing what you were doing.

Example: 

Stash with a Message
git stash push -m "WIP: homepage redesign"

Output:

Saved working directory and index state On main: WIP: homepage redesign

View All Saved Stashes

To see every stash you’ve saved:
git stash list

Example output:

stash@{0}: On main: WIP: homepage redesign
stash@{1}: WIP on main: 1234567 Add new feature
This command displays all your current stashes along with their index and description, so you can remember what each stash contains.

View Stash Details in Git

When you’ve saved changes with git stash, you might want to see what exactly you stashed. Git makes this easy.

Show a Summary of the Latest Stash

git stash show

Example Output:

plaintext
 src/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
This command gives you a quick summary of which files were modified and how many lines were added or removed in your most recent stash.

View a Full Diff of the Latest Stash

To see the exact lines that changed:
git stash show -p

Example Output:

diff
diff --git a/src/index.html b/src/index.html
index 1234567..89abcde 100644
--- a/src/index.html
+++ b/src/index.html
@@ ...
This shows a detailed diff, similar to what you'd see with git diff, so you can review the precise changes in your stash.

Tip:

You can also inspect older stashes by adding their reference, like git stash show stash@{1} or git stash show -p stash@{2}.

Apply the Latest Stash (git stash apply)

Restore your most recent stashed changes while keeping the stash in the stash list (so you can reuse it later):

Example:

git stash apply

Output:

rust
On branch main
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
    modified:   src/index.html
This brings back the changes you stashed most recently but does not remove the stash itself.

Apply a Specific Stash (git stash apply stash@{n})

To apply an earlier stash instead of the latest one, specify its identifier:

Example:

git stash apply stash@{1}

Output:

yaml
On branch main
Changes not staged for commit:
    modified:   src/index.html
This applies the specific stash you choose, again without removing it from the stash list.

Pop the Stash (git stash pop)

Apply the latest stash and remove it from the stash stack in one step.

Example: 

Pop the latest stash
git stash pop

Output:

text
On branch main
Changes not staged for commit:
    modified:   src/index.html
Dropped refs/stash@{0} (abc1234d5678)
This command reapplies your most recent stashed changes and automatically deletes that stash from the list.

Drop a Stash (git stash drop)

Delete a specific stash when you no longer need it.

Example: 

Drop a specific stash
git stash drop stash@{0}

Output:

text
Dropped stash@{0} (abc1234d5678)
This permanently removes the chosen stash from your stash list.

Clear All Stashes (git stash clear)

Want to remove every stash you’ve saved? You can clear them all at once:

Example:

git stash clear
Warning: This will delete all your stashes permanently. Once cleared, you can’t get them back — so use this command carefully.

 Branch from a Stash (git stash branch)

Sometimes you realize your stashed work should live on its own feature branch.
You can easily do this by creating a new branch and applying the stash in one step:

Example:

git stash branch new-feature stash@{0}

output:

csharp
Switched to a new branch 'new-feature'
On branch new-feature
Changes not staged for commit:
    modified:   src/index.html
Dropped stash@{0} (abc1234d5678)
This command does three things in one go:
  • Creates a new branch called new-feature
  • Applies your stashed changes to that branch
  • Removes the stash from the stash list

This is perfect when your stashed work deserves to become its own feature branch.

Troubleshooting Git Stash Issues

  • Lost your changes?
Run git stash list to see your saved stashes. Use git stash apply to reapply them to your working directory.
  • Stash didn’t apply cleanly?
Sometimes applying a stash can cause conflicts, similar to a merge. Git will highlight these conflicts in your files so you can resolve them manually
  • Untracked files missing?
By default, git stash doesn’t include untracked files. Next time, use git stash -u to stash them as well.
  • Cleared all stashes by mistake?
Be careful with git stash clear—it permanently deletes all stashes and cannot be undone. Always double-check before using it!












Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send