I stashed my changes. Now I want to unstash only some files from the stash. How can I do this?
问题:
回答1:
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>
(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>
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".
回答2:
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
回答3:
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
回答4:
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...
回答5:
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.
回答6:
One more way:
git diff stash@{N}^! -- path/to/file1 path/to/file2 | git apply -R
回答7:
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'