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.

stash

Stashing your work

The git stash takes uncommitted both staged and unstaged changes, saves them away for further use, and then returns 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 status
On branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage
$ git status
On branch master
nothing to commit, working tree clean

Re-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 status
On branch master
nothing to commit, working tree clean
git stash pop
On 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 -u

You can add changes to ignored files as well by using the -a option (or --all) when running git stash.

git stash -a

Multiple 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 list
stash@{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 homepage

It is good to add some context with git stash save "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 pop stash@{3}

Viewing stash diffs

Use git stash show to view a summary of a stash:

git stash show
index.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 show -p
diff --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 ask if you want to stash it or not:

git stash -p
diff --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,?]? n

Hunk 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 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 drop stash@{1}
Dropped stash@{1} (17e2697fd8251df6163117cb3d58c1f62a5e7cdb)

If you use git stash clear it will delete all the stashes:

git stash clear

Practice Your Knowledge

What are the features and functionalities of the 'git stash' command?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.