Git Stash

Imagine you are in the middle of a task, and your boss asks you to fix a urgent bug on another branch. Your work is not ready to commit. The git stash command puts away your uncommitted changes in a safe place, gives you a clean working directory, and you can bring them back later.

Syntax

git stash
git stash push -m "message"
git stash list
git stash pop
git stash apply
git stash drop
git stash -u

Stash your changes

Terminal output

$ echo "half done work" >> app.txt
$ git status -s
M app.txt
$ git stash
Saved working directory and index state WIP on main: 6b3a366 Add app
$ git status -s
$ cat app.txt
v1

The file is back to the last commit. Your change is saved in the stash.

See the list of stashes

Terminal output

$ git stash list
stash@{0}: WIP on main: 6b3a366 Add app

Bring the changes back

There are two ways. git stash pop applies the latest stash and deletes it from the list. git stash apply applies it but keeps it in the list, which is useful when you want to use it in more than one branch.

Terminal output

$ 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} (b3b1e20772ddbf4900b41a5a645a1d4663ecdc97)
$ cat app.txt
v1
half done work
$ git stash list

Stash with a message

When you have many stashes, a name helps you to know which is which.

Terminal output

$ echo "login work" >> app.txt
$ git stash push -m "login form in progress"
Saved working directory and index state On main: login form in progress
$ echo "search work" >> app.txt
$ git stash push -m "search box in progress"
Saved working directory and index state On main: search box in progress
$ git stash list
stash@{0}: On main: search box in progress
stash@{1}: On main: login form in progress

Apply a specific stash

Terminal output

$ git stash apply stash@{1}
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")
$ cat app.txt
v1
half done work
login work

Delete stashes

Terminal output

$ git stash drop stash@{1}
Dropped stash@{1} (1382f8c0cbd4d14800584a040f1a5691f748c3b3)
$ git stash list
stash@{0}: On main: search box in progress
$ git stash clear
$ git stash list

Stash untracked files too

By default stash takes only tracked files. New files (untracked) stay in the folder. To include them, use -u.

Terminal output

$ git checkout -q app.txt
$ echo "new" > newfile.txt
$ git stash
No local changes to save
$ git stash -u
Saved working directory and index state WIP on main: 6b3a366 Add app
$ git status -s

The first git stash said there was nothing to save, because newfile.txt is untracked. After -u, it was stashed.

Create a branch from a stash

git stash branch new-branch-name
Note: A stash is stored only on your computer, and it is not pushed. Do not keep important work in a stash for a long time. Make a commit on a branch instead. Next: Git Tag.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.