How to unstash only certain files?

2019-01-20 21:02发布

I stashed my changes. Now I want to unstash only some files from the stash. How can I do this?

标签: git git-stash
7条回答
放荡不羁爱自由
2楼-- · 2019-01-20 21:29

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...

查看更多
家丑人穷心不美
3楼-- · 2019-01-20 21:30

One more way:

git diff stash@{N}^! -- path/to/file1 path/to/file2  | git apply -R
查看更多
劫难
4楼-- · 2019-01-20 21:31
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
查看更多
Animai°情兽
5楼-- · 2019-01-20 21:31

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'

查看更多
【Aperson】
6楼-- · 2019-01-20 21:34

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
查看更多
beautiful°
7楼-- · 2019-01-20 21:37

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.

查看更多
登录 后发表回答