Back to roadmaps git Course

Saving Work in Progress with git stash

Sometimes you are halfway through coding a feature when a critical bug is reported on your production branch. You must switch branches to fix it, but you do not want to make a messy, incomplete commit. git stash solves this by temporarily saving your work.


1. Stashing and Restoring Changes

To save your current modifications (both staged and unstaged) and return your working directory to a clean state:

# Save uncommitted changes to the stash stack
git stash

Your working directory is now clean. You can switch branches, fix the bug, commit, and return.

Restoring Your Saved Work

Once you return to your feature branch, retrieve your saved modifications:

# Re-apply the last stashed changes and remove them from the stash list
git stash pop

Alternatively, you can apply the changes without removing them from the stash:

# Re-apply changes but keep them in the stash stack
git stash apply

2. Managing the Stash Stack

You can save multiple stashes. Use these commands to manage the stack:

# List all stashed changes
git stash list

# Stash changes with a custom description
git stash save "layout work-in-progress"

# Discard the most recent stash
git stash drop

# Clear the entire stash stack list
git stash clear
Published on Last updated: