git stash
On this page you will find useful information about git stash command and how to stash your work, as well as learn about multiple and partial stashes.
Definition
The git stash command shelves changes you have made to your working copy so you can do another work, and then come back and re-apply them.

Stashing your work
The git stash command takes both uncommitted staged and unstaged changes, saves them away for further use, and then removes them from your working copy. Firstly, you can run the git status so you can see the dirty state. Then run git stash to stash the changes:
Git stash
git statusOn branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.htmlGit stash
$ git stashSaved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepageGit stash
$ git statusOn branch master
nothing to commit, working tree cleanRe-applying your stashed changes
The git stash pop removes the changes from your stash and re-applies them to your working copy.
The alternate way is running git stash apply if you want to re-apply the changes and keep them in your stash:
Git stash
git statusOn branch master
nothing to commit, working tree cleanGit stash
git stash popOn branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{0} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)Stashing untracked or ignored files
The git stash will stash the changes that have been added to your index (staged changes) and changes made to files currently tracked by Git (unstaged changes). It will not stash the new files in the working copy that have not yet been staged and ignored files. In these cases, the git stash -u option (or --include-untracked) helps to stash the untracked files.
Git stash
git stash -uYou can add changes to ignored files as well by using the -a option (or --all) when running git stash.
Git stash
git stash -aMultiple Stashes
You can run git stash several times so as to create multiple stashes, and then run git stash list to view them. By default, stashes are identified as a "WIP" – work in progress. It comes on top of the branch and commits that you created the stash from.
Git stash
git stash liststash@{0}: WIP on master: 5002d47 our new homepage
stash@{1}: WIP on master: 5002d47 our new homepage
stash@{2}: WIP on master: 5002d47 our new homepageIt is good to add some context with git stash push -m "message"
By default, git stash pop will re-apply the last created stash: stash@{0}
You can choose which stash to re-apply like this:
Git stash
git stash pop stash@{3}Switched to a new branch 'add-stylesheet'
On branch add-stylesheet
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{1} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)Viewing stash diffs
Use git stash show to view a summary of a stash:
Git stash
git stash showindex.html | 1 +
style.css | 3 +++
2 files changed, 4 insertions(+)You can also use the -p or --patch options to see the full diff of a stash:
Git stash
git stash show -pdiff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+<link rel="stylesheet" href="style.css"/>Partial stashes
Git allows choosing whether you want to stash just a single file, a bunch of files or individual changes within files. The git stash -p iterates through each hunk (a piece of change in Git) in the working copy and asks if you want to stash it or not:
Git stash
git stash -pdiff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
Stash this hunk [y,n,q,a,d,/,e,?]? y
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+<link rel="stylesheet" href="style.css"/>
Stash this hunk [y,n,q,a,d,/,e,?]? nHunk Commands
| Command | Description |
|---|---|
| / | Search for a hunk by regex. |
| ? | Print help. |
| n | Do not stash the hunk. |
| a | Stash this hunk and all later hunks in the file. |
| d | Do not stash this hunk or any of the later hunks in the file. |
| e | Manually edit the current hunk |
| q | Quit (selected hunks will be stashed) |
| s | Split the hunk into smaller hunks. |
| y | Stash the hunk. |
Creating a branch from stash
You can create a new branch to apply your stashed changes to it with git stash branch:
Git stash
git stash branch add-stylesheet stash@{1}Switched to a new branch 'add-stylesheet'
On branch add-stylesheet
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{1} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)Cleaning up the stash
You can delete the stash with git stash drop:
Git stash
git stash drop stash@{1}Dropped stash@{1} (17e2697fd8251df6163117cb3d58c1f62a5e7cdb)If you use git stash clear it will delete all the stashes:
Git stash
git stash clearPractice
What are the features and functionalities of the 'git stash' command?