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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.