Saving current directory changes
Having code of logging at a certain commit which you want to save for later.
Having code snippets for an incomplete small fix/cleanup that shouldn't belong to a separate branch, yet it's not a current priority.
Having to do a certain change and try those changes and get back to the development you've been into.
Having to checkout and test the code at a certain point of time while working to compare the code output.
HEAD -> resembles the last committed changes
Index/cache -> resembles the staged changes (marked green when we use git status
)
No index -> resembles uncached changes (may include tracked, untracked or removed files)
List the stashes we have
Save a new stash
git stash save "stash_name"
In case we're in hurry we may omit the save and message name
Restore (Pop) the previous stash into the current directory
Note: we may figure the stash index using git stash list
.
Note: stash index changes everytime we pop/save a new stash.
In case we're in a hurry for restoring the previous stash
Note: git stash pop
removes the stash from list, git stash apply
doesn't.
To show the previous stash content*
Note: without the -p
options we're left with the stash stats only
git stash show -p stash@{0}
*untracked files won't be showed in the previous command, as they are stored in the third parent of the stash
This question tackles this point
This is the commit that introduced the -u(--include-untracked) option
-p|--patch
-> interactive mode
--no-keep-index
-> stash (staged + unstaged) files
--keep-index
-> stash unstaged files (default with --patch option)
-u|--inlcude-untracked
-> include untracked files (those are unstaged files)
-a|--all
-> include ignored files
Note: we can't combine (--patch
and --all
) or (--patch
and --include-untracked
)
In order to use the interactive mode with untracked/ignored files, stage all the files of interest for the stash using git add
and go interactive with those files
My Best Command
git stash save -p --no-keep-index "stash message"
If we don't want to stage and have no problem with getting whole files at once (non-interactive mode) we may use
git stash save --no-keep-index --include-untracked "stash message"
If we don't want to touch the staged
git stash save --keep-index --include-untracked "stash message"
Using --all
will result in stashing stuff we don't really care about, typically we don't wanna use it.
Extra Readings
git stash documentation