Patrique Ouimet
Senior Product Engineer
Sun, Jul 26, 2020 9:18 AM
From the output of git stash --help
:
"Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit."
In simpler terms, if you're making changes and want to store them (and return your branch to a clean state [aka last commit]) without having to commit the changes use git stash
. When you need those changes back again simply use git stash pop
. Something to note is git stash
will only include files that have been staged, so if you add a new file and want it to be part of the stash you'll have to git add
that file (or directory) before running git stash
.
git stash
git stash push -m "WIP intro paragraph"
git stash push --message "WIP intro paragraph"
git stash pop
NOTE: index 1
in the example below can be found when running git stash list
command
git stash pop 1
git stash list
NOTE: index 1
in the example below can be found when running git stash list
command
git stash show 1
You have work in progress but not ready to be made into a commit but you need to switch to another branch (i.e. hotfix).
➜ git-demo git:(update-home-page) ✗ git status
On branch update-home-page
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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
➜ git-demo git:(update-home-page) ✗ git stash
Saved working directory and index state WIP on update-home-page: f2586a9 Initial setup
➜ git-demo git:(update-home-page) git checkout master
Switched to branch 'master'
➜ git-demo git:(master) git checkout -b hot-fix-123
Switched to a new branch 'hot-fix-123'
➜ git-demo git:(hot-fix-123) git status
On branch hot-fix-123
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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
➜ git-demo git:(hot-fix-123) ✗ git commit -am "Hot fix"
[hot-fix-123 97f8735] Hot fix
1 file changed, 2 insertions(+), 2 deletions(-)
➜ git-demo git:(hot-fix-123) git push origin hot-fix-123
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 291 bytes | 291.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'hot-fix-123' on GitHub by visiting:
remote: https://github.com/patoui/git-demo/pull/new/hot-fix-123
remote:
To github.com:patoui/git-demo.git
* [new branch] hot-fix-123 -> hot-fix-123
➜ git-demo git:(hot-fix-123) git checkout update-home-page
Switched to branch 'update-home-page'
➜ git-demo git:(update-home-page) git stash pop
On branch update-home-page
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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9e41e4b0d4fd215bbc761c4305f4f450d029775a)