GeekDas
2 min read
GeekdasGeekdas

Git Stash: Save Your Work Without Committing

If you’ve ever wondered for “what is git stash”, “what does git stash do”, or looked for a simple git stash tutorial, this guide will explain everything in clear and practical terms.

While working on a project, sometimes you need to switch branches or pull new changes, but your work is not ready to commit yet. This is where Git Stash becomes very useful.

Git stash lets you temporarily save your uncommitted changes, clean your working directory, and then bring those changes back later.

What is Git Stash?

'git stash' takes your modified files (staged and unstaged) and stores them in a safe place. Your working directory becomes clean, just like the last commit.

You can think of stash as a clipboard for your code.

Basic Git Stash Commands:

1. Stash your changes

This saves all your uncommitted changes and resets your working directory.

git stash

2. Stash with a message

Adding a message helps you remember why you stashed the changes.

git stash push -m "work in progress on login page"

3. View all stashes

git stash list

example output

stash@{0}: On main: work in progress on login page
stash@{1}: On feature: navbar UI update

4. Apply the latest stash

This restores the changes but keeps the stash in the list.

git stash apply

5. Apply and remove the stash

This restores the changes and deletes the stash after applying.

git stash pop

6. Apply a specific stash

git stash apply stash@{1}

7. Drop a stash

This deletes a specific stash.

git stash drop stash@{0}

8. Clear all stashes

This removes all stashed changes permanently.

git stash clear

Example Usage Scenario

Imagine you are working on a feature and suddenly your manager asks you to fix a production bug.

Step 1: Save your current work

git stash push -m "halfway login form"

Step 2: Switch branch and fix the bug

git checkout main

Step 3: Come back and restore your work

git checkout feature/login
git stash pop

Your unfinished work is back exactly as it was.

When Should You Use Git Stash?

  • When you need to quickly switch branches

  • When your work is not ready to commit

  • When pulling new changes causes conflicts

  • When you want to keep your commit history clean

Conclusion

Now you clearly understand:

  • What is git stash

  • What is stash in git

  • What does git stash do

  • How to use git stash command

  • A practical git stash example

Git stash is a powerful feature that helps you manage unfinished work without committing it. By using stash correctly, you can work faster, switch contexts easily, and keep your repository clean.

If you're learning Git, mastering git stash is an essential skill.

Frequently Asked Questions

What is git stash in simple words?
Git stash is a command that temporarily saves your uncommitted changes so you can work on something else without committing unfinished code.
What does git stash do exactly?
The git stash command stores your modified files in a hidden stack and restores your working directory to the last commit state.
When should I use git stash?
You should use git stash when switching branches, pulling updates, or handling urgent fixes while your current work is incomplete.
What is the difference between git stash apply and git stash pop?
"git stash apply" restores changes but keeps the stash saved. "git stash pop" restores changes and removes the stash from the list.
Does git stash save staged files?
Yes, git stash saves both staged and unstaged changes by default.
Is git stash permanent?
No. Git stash is temporary. If you run "git stash clear", all saved stashes are permanently deleted.
Can I stash only specific files?
Yes. You can stash specific files using: git stash push file-name

Related Articles