How to use the Git stash command for saving temporary changes? Explain with examples.


Stash refers to the act of storing something in a secret location. Git provides a stash option to temporarily store uncommit changes and restore the development line to a point where the repository is clean. The option enables context switches while the current development is still incomplete.

When do stashing?

A clean codebase is required to pull code from a remote location, merge a branch, or switch to another branch.

GIT stash command

The following you may need to know before using the stash command.

When to use the git stash?

To develop a feature, you must clone the repository and add new code. However, a new requirement requires immediate code changes on top of the remote codebase. Changes to feature code are not permanent and cannot be committed. So what are we to do?

One method is to check out the code in a new location and make changes and push. After that, switch to work with the original local directory having temporary changes.

You can use the git stash command to save local changes and revert to the code’s original state before performing a git pull to retrieve the changes. You can later apply the local changes made during the update.  This will not add any dirty commitment to the working code line.

How to use git stash explained with example?

Check what local changes have been done with the git status command and save them with the git stash.

Check what has been modified.

# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

The above changes are temporary, and you can not commit, but you must go to the original state for a quick fix. Here git stash will do the job.

Use git stash to save temporary changes.

#git stash
Saved working directory and index state WIP on master: cf3ea37 Merge https://github.com/cspsprotocol/projectsource
HEAD is now at cf3ea37 Merge https://github.com/cspsprotocol/projectsource

Verify that the working copy is clean.

# git status
# On branch master
nothing to commit, working directory clean

Check what has been stashed.

# git stash list
stash@{0}: WIP on master: cf3ea37 Merge https://github.com/cspsprotocol/projectsource

Show that we have only one entry. If there are multiple points at which you have stashed the changes, it will look like the following.

stash@{0}: 
stash@{1}:
stash@{2}:
stash@{3}:

Greater the index older the saved point; the most recent one is zero.

How to move back to the temporary change?

Once the bug is fixed, it’s time to move to the original point that you have left.

Bring changes from the temporary location to the working tree.

# git stash pop
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (7184c69236e4926f9fc8c78aa35ff705eb5a1e6e)

Check if temporary changes are applied.

]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

The pop will apply and remove the latest saved change from the list.

# git stash list

To apply a specific staged element, you can do that by specifying the index number.

 #git stash pop stash@{n}

Here n is the index number.