Skip to content
Related Articles

Related Articles

Git | Working with Stash

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 27 Feb, 2020

Git allows multiple users to work on the same project simultaneously. Suppose if a developer is working on a feature in a branch and he needs to pull changes from some other developer’s branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot commit the partial code of the currently working feature. To add this new feature, you have to remove your current changes and store them somewhere else. For this type of situation, Git offers a very useful command known as ‘git stash‘.
git stash command saves the previously written code and then goes back to the last commit for a fresh start. Now you can add the new feature without disturbing the old one as it is saved locally. After committing the new feature you can go on with working on the old feature which was incomplete and uncommitted.

Stashing current changes

To save the uncommitted changes for later usage, you can use the git stash command. This command saves your local modifications away and reverts the working directory to match the HEAD commit, so it will give you clean working directory.

git stash

git stash
By default, running git stash will stash the changes that have been added to your index (staged changes) and changes made to files that are currently tracked by Git (unstaged changes). To stash your untracked files, use git stash -u.

Listing Multiple Stashes

You can create multiple stashes and view them using the git stash list command. Each stash entry is listed with its name (e.g. stash@{1}), the name of the branch that was current when the entry was made, and a short description of the commit the entry was based on.

git stash list

git stash list
To provide more context to the stash we create the stash using the following command:

git stash save "message"

Getting back the Stashed changes

You can reapply the previously stashed changes with the git stash pop or git stash apply command. The only difference between both the commands is that git stash pop removes the changes from the stash and reapplies the changes in the working copy while git stash apply only reapplies the changes in the working copy without removing the changes from the stash. In simple words, “pop” removes the state from the stash list while “apply” does not remove the state from the stash list.

git stash pop

git stash pop

git stash apply

git stash apply
By default git stash pop or git stash apply will reapply the most recently created stash: stash@{0}
To choose which stash to apply, you can pass the identifier as the last argument (For eg.:- git stash pop stash@{2}).

Viewing Stash Summary

git stash show command is used to display the summary of operations performed on the stash.

git stash show

git stash show

Creating a Branch from stash

If you want to create and check out a new branch starting from the commit at which the stash was originally created and also apply the changes saved in stash, use git stash branch branch_name stash_name. It drops the stash which is given as the argument and if no stash is given, it drops the latest one.

git stash branch newbranch stash@{0}

git stash branch

Deleting Stashes

To delete any particular stash (For ex:– stash@{1}), use git stash drop stash@{1}. By default this command will drop stash@{0} if no argument is provided (git stash drop).
To delete all stashes at once, use git stash clear command.
git clear & git drop

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!