I'd like to know if it is possible to extract a single file or diff of a file from a git stash without popping the stash changeset off.
Might anyone be able to provide some suggestions/ideas about this?
I'd like to know if it is possible to extract a single file or diff of a file from a git stash without popping the stash changeset off.
Might anyone be able to provide some suggestions/ideas about this?
Notes:
Make sure you put space after the "--" and the file name parameter
Replace zero(0) with your specific stash number. To get stash list, use:
Based on Jakub Narębski's answer -- Shorter version
In git stash manpage you can read that (in "Discussion" section, just after "Options" description):
So you can treat stash (e.g.
stash@{0}
is first / topmost stash) as a merge commit, and use:Explanation:
stash@{0}^1
shortcut means first parent of given stash, which as stated in explanation above is commit at which changes were stashed away. We use this form of "git diff" (with two commits) becausestash@{0}
/refs/stash
is a merge commit, and we have to tell git which parent we want to diff against. More cryptic:should also work (see git rev-parse manpage for explanation of
rev^!
syntax, in "Specifying ranges" section).Likewise, you can use git checkout to check a single file out of the stash:
or to save it under another filename:
or
(note that here <full filename> is full pathname of a file relative to top directory of a project (think: relative to
stash@{0}
)).You might need to protect
stash@{0}
from shell expansion, i.e. use"stash@{0}"
or'stash@{0}'
.Short answer
To see the whole file:
git show stash@{0}:<filename>
To see the diff:
git diff stash@{0}^1 stash@{0} -- <filename>
There is an easy way to get changes from any branch, including stashes:
You may omit the file spec if you want to patch in many parts. Or omit patch (but not the path) to get all changes to a single file. Replace
0
with the stash number fromgit stash list
, if you have more than one. Note that this is likediff
, and offers to apply all differences between the branches. To get changes from only a single commit/stash, have a look atgit cherry-pick --no-commit
.If the stashed files need to merge with the current version so use the previous ways using diff. Otherwise you might use
git pop
for unstashing them,git add fileWantToKeep
for staging your file, and do agit stash save --keep-index
, for stashing everything except what is on stage. Remember that the difference of this way with the previous ones is that it "pops" the file from stash. The previous answers keep itgit checkout stash@{0} -- <filename>
so it goes according to your needs.If you use
git stash apply
rather thangit stash pop
, it will apply the stash to your working tree but still keep the stash.With this done, you can
add
/commit
the file that you want and then reset the remaining changes.