Summary of git-stash usage

  • A class was found to be redundant, and to delete it, you were worried that you would need to view its code later, you wanted to save it, but you didn't want to add a dirty submission.You can then consider git stash.
  • When using git, we often use branch to solve task switching problems. For example, we often build our own branch to modify and debug code. If someone else or we find a bug in the original branch that has to be modified, we usually submit half of the completed code commit to the local repository, and then switch the branch to modify the bug to change it.Switch back later.In this case, there are often a lot of unnecessary records on the log.In fact, if we don't want to submit half or imperfect code, but have to modify an emergency bug, you can use git stash to push your code that isn't currently submitted locally (and on the server) into Git's stack, when your workspace is exactly the same as what was submitted last, so you can safely fix the bug until it's finished, and then use git stash apply to apply the previous half of your work back to the server.
  • It often happens that when you're working on a part of a project, it's a bit messy and you want to move on to another branch to do some work.The problem is that you don't want to submit half the work or you won't be able to return to this point of work in the future.The solution to this problem is the git stash command.Stash takes the intermediate state of your working directory -- that is, the files you modified and temporary changes you made -- and saves it on a stack of unfinished changes that can be reapplied at any time.


It is important to note that stash is local and will not be uploaded to git server via the git push command.

1. Current stash modifications

$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

In practice, it is recommended to add a message to each stash to record versions

$ git stash save "test-cmd-stash"
Saved working directory and index state On autoswitch: test-cmd-stash
HEAD Now at 296 e8d4 remove unnecessary postion reset in onResume function
$ git stash list
stash@{0}: On autoswitch: test-cmd-stash

2. Re-apply cached stash

1. Delete the first stash in the cache stack and apply the corresponding modifications to the current working directory
$ git stash pop
2. You can use the git stash apply command to apply stash from the cache stack to the working directory multiple times without deleting the stash copy
$ git stash apply

You can use a name to specify which stash to use, and by default, the closest stash (that is, stash@{0})

3. View existing stash
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
4. Remove stash
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
5. View the diff of the specified stash

You can use the git stash show command followed by the stash name.Examples are as follows:

$ git stash show
 index.html | 1 +
 style.css | 3 +++
 2 files changed, 4 insertions(+)
//Add -p or--patch after this command to see all diff for a particular stash, as follows:

$ 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"/>
6. Create branches from stash
$ git stash branch testchanges
Switched to a new branch "testchanges"
# On branch testchanges
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#      modified:   index.html
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#      modified:   lib/simplegit.rb
Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)

This command creates a new branch with the modifications in stash and deletes the stash when it is created successfully

7. Stay untracked or ignored files

By default, git stash caches the following files:

Modifications added to staging area (staged changes)
Git tracked changes that were not added to the staging area
But the file will not be cached:

New files in the working directory (untracked files)
ignored files

The git stash command provides parameters for caching both types of files.stash untracked files can be used with -u or -include-untracked.Use the -a or -all commands to stash all changes in the current directory.

Reference resources: Summary of git-stash usage

Keywords: git

Added by kkeim on Thu, 23 Jan 2020 09:50:00 +0200