有可能吗?
是的!
git checkout -p stash@{0}
您可以将stash@{0} 中的0 替换为您要应用的存储的索引。
如果您不确定要应用哪个 n,请使用 git stash list 和 git show -p stash@{n}。
当您知道不再需要该存储时,不要忘记git stash drop stash@{n},因为git checkout 显然不会为您丢弃存储。
为什么有效?
关键是要意识到,stash 本质上是references 到commits,就像标签和分支一样。
确实,它们存储在.git/refs/stash 中,每个存储散列一行。
注意事项
正如下面 cmets 中提到的@mgadda,git checkout -p 尝试应用提交和当前工作空间之间的全部差异。
在 git stash 的情况下,如果您尝试应用的 stash 是针对不同的提交完成的,那么 git checkout -p stash@{n} 将尝试以交互方式应用提交 stash@{n} 和提交之间的所有差异当前工作区,包括所有不同的父提交。
例如,如果您尝试将“多次提交前”保存的存储应用到当前工作区,git checkout -p stash@{n} 将尝试不仅应用存储中的更改,还会尝试 还原在存储所基于的提交和当前提交之间发生的所有更改。
相反,如果您尝试“从未来”应用存储,即应用到一个分支中,该分支是在存储所基于的提交之前的多次提交,那么git checkout -p stash@{n} 将尝试也应用在当前提交和未来提交之间发生的所有其他更改,除了存储本身的更改。
(如果您想知道,git checkout -p stash@{n} 来自并行分支的存储将尝试还原当前提交和原始分支点之间的所有更改并且也会应用分支之间的所有更改point 和另一个分支,除了 stash 的变化)。
解决方法
有一些解决方法,但没有一个适合所有情况:
-
- 在执行
git checkout -p stash@{n} 时,请务必小心您接受的补丁程序
-
- 先执行
git stash pop,然后再执行git stash,然后再执行git checkout -p ...。但是,如果您想部分应用您的存储以避免冲突,这将无济于事。在这种情况下,请参阅下面的解决方案 4。
-
- 如果您有 git 支持的图形差异工具(如 meld),您可以使用
git difftool 并仅“向左应用”您感兴趣的更改:
-
-
(Based on @andrew's answer) 在一个分离的头上,返回到您感兴趣的存储的“父”提交,应用存储,仅交互地重新存储您感兴趣的部分,返回并重新应用较小的存储。
一步一步:
git checkout stash@{n}^ # notice the "^".
# Now you're in a detached head in the parent commit of the stash.
# It can be applied cleanly:
git stash apply stash@{n}
# Now save only the diffs you're interested in:
git stash -p
# remove the rest of the old stash
git checkout -- . # be careful or you could remove unrelated changes
# go back to the branch where you want to apply the smaller stash
git checkout <my previous branch>
# apply the smaller stash
git stash pop