What is git stash and when do you use it?

git stash takes your uncommitted changes and keeps them in a stack, giving you a clean working directory. You use it when you need to switch to another branch quickly, for example for an urgent bug fix, but your current work is not ready for a commit.

Terminal output

$ echo "unfinished" >> app.txt
$ git stash
Saved working directory and index state WIP on main: 0f2bd93 First
$ git stash list
stash@{0}: WIP on main: 0f2bd93 First
$ git stash pop
On branch main
Changes not staged for commit:
(use "git add <file>…" to update what will be committed)
(use "git restore <file>…" to discard changes in working directory)
modified: app.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f42418453db53316b813fc611460606e4c2b9896)

  • git stash pop brings the changes back and removes the stash.
  • git stash apply brings them back and keeps the stash.
  • git stash -u also stashes untracked files.

See Git Stash.


Learn more about the similar topics:
Tutorials
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?