stash
command takes changes in working tree and index (staging area) away and store them on a stack. You can reapply the changes later. Here it shows you how to use this command to accomplish kinds of tasks.
Stash changes
Stash all changes
To make the meaning more clear, below explanations use different words to describe what a command does.
# Stash changes in working tree and index away
$ git stash
# Save changes in working tree and index, but changes in index are not taken away.
# To make it more clear, changes in working tree and index are saved in the stash,
# and changes in working tree are taken away while those in index are not.
$ git stash --keep-index
# Stash changes, also include untracked files
$ git stash --include-untracked
# Stash changes, also include untracked and ignored files
$ git stash --all
Stash only certain files
To stash only changes in specified files or folder, run below command:
# Stash only changes in some pathspec
$ git stash -- <pathspec>
# Examples:
# Stash only changes in inc/ folder
$ git stash -- inc/
# Stash all changes in .php files inside inc/ folder
$ git stash -- inc/*.php
# Stash only changes in a.php and b.php
$ git stash -- a.php b.php
Stash only certain changes (partial changes)
If you want to stash partial changes in a file, --patch
can help you to do that. With that option, Git will show each change hunk and prompt you interactively whether you would like to stash it or keep it in your working directory.
$ git stash --patch
diff --git a/main.js b/main.js
index d13b628..8523064 100644
--- a/main.js
+++ b/main.js
@@ -506,6 +506,10 @@ function process_import_data(input_file_id, response_func) {
}
const file = files[0];
+ if (file.size > MAX_FILE_SIZE) {
+ display_message("The file exceeds the max size 2M.");
+ return;
+ }
if (!file.type.startsWith("application/vnd.ms-excel")){
display_message("The type of the file is not corrent.");
return;
Stash this hunk [y,n,q,a,d,/,e,?]? y
Saved working directory and index state WIP on master: 2a55bef added the index file
The list of promoted action options:
y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
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
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Note: The
--patch
option implies--keep-index
. You can use--no-keep-index
to override this.
You can also specify certain files to git stash --patch
to limit the scope:
$ git stash --patch -- main.js
List all stashes
# List the stashes that have been made on the curren branch
$ git stash list
stash@{0}: WIP on master: b1f092d update: hello.php
The above result shows the stash name, the branch name, and the commit this stash was based on.
Reapply stashed changes
Reapply a stash
# Apply the top stash on the stack
$ git stash apply
Use stash@{n}
to reference the n stash from the top on the stack, it is started from 0 :
# Apply the top stash on the stack
$ git stash apply stash@{0}
# Apply the stash before stash@{0} on the stack
$ git stash apply stash@{1}
Be aware that the staged(index) changes in the stash are not restaged. To do that, run:
# Revert working tree and index
$ git stash apply --index
The reapplied stash is not removed from the stack. To remove a stash, use drop
command:
# Remove the top stash on the stack
$ git stash drop stash@{0}
Or use pop
command to reapply a stash and remove it from stack:
# Repplay a stash in working tree and remove it from stack
$ git stash pop stash@{0}
# Repplay a stash in working tree and index and remove it from stack
$ git stash pop --index stash@{0}
Resolve stash merge conflicts
git stash: Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand. After the conflicts for a file are resolved, use git add
command to remove its conflict state. To remove the stash from the stash list, run git stash drop
command.
Tips
You do not have to be on the same branch to apply the changes. There will be merge conflicts if there is change that can not be applied cleanly. In this case, the files with merge conflicts will be
UU
(meaning “unmerged, both index and working tree modified”) state if you show status in short format:# src/hello.js is a file with merge confilits. # -s, gives the output in short format. $ git status -s ... UU src/hello.js
If you show status just with
git status
, these files will be showed as below:$ git status ... Unmerged paths: (use "git restore --staged ..." to unstage) (use "git add ..." to mark resolution) both modified: js/hello.js ``` What you need to do is to resolve the merge conflicts manually then use below command to mark resolution for these files: ```shell $ git add
Or use below command to unstage these files, then they will not be treated as
UU
state but normal modified state.$ git store --staged
Show content of a stash
# Show content of the top stash
$ git stash show stash@{0}
# Show cnotent of the second recent stash in patch form
$ git stash show -p stash@{1}
Clear all stashes
# Remove all stashes in the stack
$ git stash clear
Note: The stashes removed may be impossible to recover.