I stashed my changes. Now I want to unstash only some files from the stash. How can I do this?
-
5I think you have to apply the whole stash, but then you can selectively re-stash.– RichardMar 7, 2013 at 6:30
-
5@AbdouTahiri What is wrong with the stash?– alexMar 17, 2016 at 9:16
-
52@AbdouTahiri Uhhhh.. git stash is a legit feature, and extremely useful. I use it daily. Say, a coworker needs me to review something but I'm in the middle of a complex change set. I'm not going to commit a pile of broken code just so I can switch branches. I'm going to stash, switch branches, review, switch back, unstash. Do you care to elaborate on who or why git stash is supposedly "not recommended"? Just because your git stash history is muddied up and hard to read doesn't mean everyone elses is. A messy git stash set is just bad workflow, not a flaw of Git.– dudewadMay 11, 2016 at 1:25
-
10@alex Nothing. Nothing is wrong with git stash. Keep using it.– dudewadMay 11, 2016 at 1:26
-
5Possible duplicate of How would I extract a single file (or changes to a file) from a git stash?– Jeff PuckettSep 7, 2016 at 1:56
10 Answers
As mentioned below, and detailed in "How would I extract a single file (or changes to a file) from a git stash?", you can apply use git checkout
or git show
to restore a specific file.
git checkout stash@{0} -- <filename>
With Git 2.23+ (August 2019), use git restore
, which replaces the confusing git checkout
command:
git restore --source=stash@{0} -- <filename>
That does overwrite filename
: make sure you didn't have local modifications, or you might want to merge the stashed file instead.
(As commented by Jaime M., for certain shell like tcsh where you need to escape the special characters, the syntax would be: git checkout 'stash@{0}' -- <filename>
)
or to save it under another filename:
git show stash@{0}:<full filename> > <newfile>
(note that here
<full filename>
is full pathname of a file relative to top directory of a project (think: relative tostash@{0}
)).
yucer suggests in the comments:
If you want to select manually which changes you want to apply from that file:
git difftool stash@{0}..HEAD -- <filename>
# or
git checkout -p stash@{0} -- <filename>
Vivek adds in the comments:
Looks like "
git checkout stash@{0} -- <filename>
" restores the version of the file as of the time when the stash was performed -- it does NOT apply (just) the stashed changes for that file.
To do the latter:
git diff stash@{0}^1 stash@{0} -- <filename> | git apply
(as commented by peterflynn, you might need | git apply -p1
in some cases, removing one (p1
) leading slash from traditional diff paths)
As commented: "unstash" (git stash pop
), then:
- add what you want to keep to the index (
git add
) - stash the rest:
git stash --keep-index
The last point is what allows you to keep some file while stashing others.
It is illustrated in "How to stash only one file out of multiple files that have changed".
-
6This doesn't work if you can't
git stash pop
due to file conflicts. Balamurugan A's answer did the trick for me in that case.– AndreyDec 22, 2014 at 15:02 -
1Of if you want to select manually which changes you want to apply from that file you can use git difftool stash@{0}..HEAD -- <filename>– yucerJun 29, 2016 at 10:19
-
6Looks like "git checkout stash@{0} -- <filename>" restores the version of the file as of the time when the stash was performed -- it does NOT apply (just) the stashed changes for that file. To do the latter: "git diff stash@{0}^1 stash@{0} -- <filename> | git apply"– VivekOct 13, 2016 at 7:17
-
1@JaimeM. Thank you. I have included your comment in the answer for more visibility.– VonCSep 4, 2017 at 17:03
-
1Assuming that
unstash
meanspop
, as it seems to me that it probably would in most cases, this only partially answers the question. How do you then remove the selectively applied pieces from the stash in order to avoid later conflicts and/or confusion when popping the remaining changes? Oct 16, 2017 at 21:01
git checkout stash@{N} <File(s)/Folder(s) path>
Eg. To restore only ./test.c file and ./include folder from last stashed,
git checkout stash@{0} ./test.c ./include
-
13This is the correct answer! Single line command to apply only stashed changes from specific files, works like a charm!– 4levelsJun 20, 2014 at 13:23
-
2To apply (just) the stashed changes for the file: "git diff stash@{N}^1 stash@{N} -- <filename> | git apply"– VivekOct 13, 2016 at 7:19
-
This works for me too. Just selectively checkout the files needed to use from stash and you are done. I was stuck in this a I had used
-a
flag while creating the stash. Oct 17, 2017 at 18:18 -
1@4levels I think "apply stashed changes" is not what happens, right? I think "overwrite whatever you have with the copy from stash" is what happens.– msouthNov 5, 2019 at 16:51
I think VonC's answer is probably what you want, but here's a way to do a selective "git apply":
git show stash@{0}:MyFile.txt > MyFile.txt
-
4This might be what you want in some cases, but beware that this command will overwrite rather than merge with any working directory changes.– RhubbarbNov 5, 2015 at 12:06
-
This works for me, since I just wanted to copy a file that exists only in the stash and didn't care to
checkout
anything. Oct 26, 2016 at 5:33 -
1For Windows PowerShell:
git show stash@`{0`}:Path/To/MyFile.txt |sc Path/To/MyFile.txt
- the backticks are necessary for PS to not interpret the braces specially, and thesc
is necessary because PS's>
operator defaults to UTF-16 (actually UCS-2) which is probably not what you want. @Balamurugan A's answer doesn't suffer from these issues.– Ian KempJun 9, 2017 at 6:48
First list all the stashes
git stash list
↓
stash@{0}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ändern
stash@{1}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ändern
stash@{2}: WIP on master: 7e450c81 Merge branch 'Offlineseite'
Then show which files are in the stash (lets pick stash 1):
git stash show 1 --name-only
//Hint: you can also write
//git stash show stash@{1} --name-only
↓
ajax/product.php
ajax/productPrice.php
errors/Company/js/offlineMain.phtml
errors/Company/mage.php
errors/Company/page.phtml
js/konfigurator/konfigurator.js
Then apply the file you like to:
git checkout stash@{1} -- <filename>
or whole folder:
git checkout stash@{1} /errors
It also works without --
but it is recommended to use them. See this post.
It is also conventional to recognize a double hyphen as a signal to stop option interpretation and treat all following arguments literally.
-
1I tried many ways and this way was the one I needed. I was faced with a problem that
git stash pop
thrown an error for untracked files. thank you. Dec 23, 2018 at 13:31
One more way:
git diff stash@{N}^! -- path/to/file1 path/to/file2 | git apply -R
-
4This is the only correct answer IMO. Using
checkout
orshow
will blindly overwrite your file instead of just applying changes. "One more way" is an understatement. Dec 8, 2019 at 0:21 -
3I'm not getting the
path/to/file2
as the you want to diff against the same file in your workspace. Leaving out the second path works fine for me. - And I get an error message using the-R
option ("apply the patch in reverse", trying to patch the stashed version?!). So my working version looks likegit diff stash@{N}^! -- path/to/file | git apply -
.– ThomasHJan 15, 2020 at 15:36 -
2
-
-
The ^! did not work in my case, so I ended up doing
git diff stash@{N} -- path/to/file | git apply -R
Apr 16, 2021 at 15:34
For Windows users: curly braces have special meaning in PowerShell. You can either surround with single quotes or escape with backtick. For example:
git checkout 'stash@{0}' YourFile
Without it, you may receive an error:
Unknown switch 'e'
If you git stash pop
(with no conflicts) it will remove the stash after it is applied. But if you git stash apply
it will apply the patch without removing it from the stash list. Then you can revert the unwanted changes with git checkout -- files...
-
2To clarify the conflicts part of this post, if you
git stash pop
and there ARE conflicts, you will have to fix them manually and the stash will NOT be removed. Sep 22, 2016 at 19:00
For examle
git stash show --name-only
result
ofbiz_src/.project
ofbiz_src/applications/baseaccounting/entitydef/entitymodel_view.xml
ofbiz_src/applications/baselogistics/webapp/baselogistics/delivery/purchaseDeliveryDetail.ftl
ofbiz_src/applications/baselogistics/webapp/baselogistics/transfer/listTransfers.ftl
ofbiz_src/applications/component-load.xml
ofbiz_src/applications/search/config/elasticSearch.properties
ofbiz_src/framework/entity/lib/jdbc/mysql-connector-java-5.1.46.jar
ofbiz_src/framework/entity/lib/jdbc/postgresql-9.3-1101.jdbc4.jar
Then pop stash in specific file
git checkout stash@{0} -- ofbiz_src/applications/baselogistics/webapp/baselogistics/delivery/purchaseDeliveryDetail.ftl
other related commands
git stash list --stat
get stash show
For windows user to avoid Unknown switch 'e'
the stash@{0}
needs to surround inside quote as shown below
git restore --source='stash@{0}' -- <filename>
git stash apply // apply all files
git reset . // reset all
git add <wanted_file> // add only wanted file(s)
git checkout . // negate changes of the other files
-
3Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Aug 30 at 0:18