用例场景如下:
git checkout branch-1
[introduce some changes I don't want to commit (but want to preserve)]
git stash save "my useful changes"
git checkout branch-2
[do some business with branch-2]
git checkout branch-1
git stash apply # the last stash which is "my useful changes"现在,假设我想再次回到branch-2。我几乎肯定没有引入任何新的更改,所以我不想创建另一个存储,如果变化是相同的。我还想用同样的信息藏起来。
我可以手动检查更改是否相同(比较差异)。然后,如果它们确实是相同的,或者创建一个新的git reset / git stash; git stash drop,如果它们不是。
有办法更自动地做到这一点吗?
不够好的解决办法:
git stash,它就会创建一个新的存储(即使内容和消息与顶级存储的内容和消息相同)。git stash pop之后都使用branch-1,那么我将丢失存储消息,所以每次都必须重新键入。你总是可以说“写一个脚本,它是自动的”,这是真的。我想知道在git中是否有集成的特性。所以我认为一个简单的、有充分根据的“不”是一个很好的答案。
发布于 2015-11-05 17:48:06
为此您可以使用git指数。
从你停下来的地方:
$ git checkout branch-1
$ git stash apply # the last stash which is "my useful changes"
$ git add -A
# ... work on branch-1
$ git status如果您开始使用的状态有任何更改,您将看到一些文件部分未分阶段:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: <file>
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: <file>
#如果是这样的话,那么您需要更新存储。否则,您可以简单地进行硬重置并继续使用现有的存储。
或者,您可以在将更改保存到存储(使用git stash -k)之前将更改添加到索引中,并使用git stash apply --index应用存储库--这将在切换分支后保存一个命令。
https://stackoverflow.com/questions/33220626
复制相似问题