What is git stash?

what is git stash?
what is git stash?

Git is currently an essential tool for any programmer now. It is an open-source distributed version control developed by Linus Torvalds to help manage Linux kernel development. It can efficiently handle version management of large to small projects. In the 2021 StackOverflow Developer Survey, 90% of 76,253 participants use git for version control. This article will walk through git stash with detailed explanations and examples. It assumes you have a good understanding of how git works.

git stash usecase

In our day-to-day work when using git, we often switch branches for completing our tasks and fixes. Consider such a scenario, when a project is being developed on the dev branch, and there is a bug in the product that needs a fix urgently, but the code changes being developed are not completed, and you don’t want to commit it yet. This is where we can use the stash command to save the uncommitted code into the git stack area (unfinished changes), and then you can smoothly switch to the hotfix or respective branch to fix bugs. Once the fix is completed, we can switch back to dev and restore the code changes from the stack using git stash pop or git stash apply.

Another scenario would be, the code fix or code enhancement has been developed on the Master branch instead of the Dev branch. say if the code changes need to be switched back to the Dev branch for further development. In this case, you can save the code changes to the stack and switch back to the Dev branch and restore the content again.

If you want to know, How to setup gitignore. Refer here

git stash your work

As seen above stashing will save uncommitted changes (staged and unstaged) to the stack area.  An example is as follows

$ git status   
On branch NeuralNetworks

Your branch is up to date with 'origin/NeuralNetworks'.

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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")



$ git stash
Saved working directory and index state WIP on master: d685786 first commit

$ git status
On branch NeuralNetworks
Your branch is up to date with 'origin/NeuralNetworks’.

nothing to commit, working tree clean

How to stash with name

Please note that stashing your work will be retained locally. It is highly recommended to add a custom message to each stash to record the version. An example is as follows

$ git stash save "test-stash-v1"
Saved working directory and index state On NeuralNetworks: test-stash-v1

How to recover stashed uncommitted changes

There are two ways we can restore the uncommitted changes 

  1. git stash pop – This command will remove the first stash from the stack and restore the corresponding changes to the working directory.
  2. git stash apply – This command will apply the stash from the stack multiple times to the working directory without deleting the stash copy.

An example is as follows

$ git stash pop
On branch NeuralNetworks
Your branch is up to date with 'origin/NeuralNetworks'.

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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (aadf9b0532917995059299eafe7e2f5dbb8685b3)
$ git stash apply
On branch NeuralNetworks
Your branch is up to date with 'origin/NeuralNetworks'.

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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

How to view the existing stash

git stash list   – This command will result in a typical output is as follows 

$git stash list                
stash@{0}: On NeuralNetworks: test-stash-v2
stash@{1}: On NeuralNetworks: test-stash-v1
Note: When using the git stash apply command, you can specify which version you need by name, if nothing is specified the latest version is used by default (ie stash@{0}).

How to remove stash 

  1. git stash drop   – This command will delete specific versions of cached stash.
  2. git stash clear – This command will delete all cached stash. 
Synopsis: git stash drop stash@{0}

An example is as follows

git stash drop
Dropped refs/stash@{0} (f00b7457270978553ce8d94569e7b0d84f41f867)

How to see the diffstat against a cached stash

git stash show -p   – This command will result in showing the diffstat by the stash name. An example is as follows

Synopsis: git stash show -p stash@{1}.  
$ git stash show              
 readme.txt | 4 ++++
 1 file changed, 4 insertions(+)
$ git stash show -p
diff --git a/readme.txt b/readme.txt
index 9daeafb..a3f01e4 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,5 @@
 test
+test1
+test2
+test3
+test4

How  to create a branch from stash name

This command will result in creating a new branch with the chosen version and deletes the respective version of stash automatically. An example is as follows

Synopsis: git stash branch branchname stash@{index}
$git stash branch ModelPrepChanges
Switched to a new branch 'ModelPrepChanges'
On branch ModelPrepChanges
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:   readme.txt


no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8e8963a48e4be09a94a8fe413b29cf56cd9dd009)

Conclusion

Stash command is to save and restore the code changes that have not been committed to local (and server). Also, the code changes which are stashed can be restored to the original development branch and also to any other designated branch. The scope of stashing includes the contents of the work area (untracked files) and the staging area (going to be a part of the next commit).

Reference

  1. Official Documentation
  2. Resolve merge conflicts – Stackoverflow

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top