为什么 git format-patch 不适用于存储?
如果我运行
git format-patch -1 stash@{0}
git 会默默返回而不创建任何文件。为什么会出现这种情况?如何以与 git am 兼容的格式保存存储?
If I run
git format-patch -1 stash@{0}
git returns silently without creating any file. Why does this happen? How can I save a stash in a format compatible with git am
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎是因为存储提交被表示为合并(在其父级和当时的索引状态之间),而合并提交上的
format-patch
不执行任何操作。如果你
这么说,它会在藏品和每个父母之间吐出补丁。
为了便于说明,藏匿处如下所示:
This seems to be because the stash commit is represented as a merge (between its parent and the index state at the time), and
format-patch
on a merge commit does nothing.If you say
then it will spit out patches between the stash and each parent.
For illustration, this is what the stash looks like:
您可以尝试
这将在您的桌面上生成最新补丁和原始父补丁的补丁文件。
在 show 选项下的 git-stash 文档 中进行了描述
You could try
This will generate a patch file on your desktop for the latest patch and the original parent.
Described in the documentation of git-stash under the show option
我可以想到两种方法。
解决方案 1:从存储中创建一个分支。这违背了存储功能的最初目的,即避免创建单独的分支。
解决方案 2:在存储之前将对跟踪文件的所有更改添加到索引中。存储后,运行
将 HEAD 与索引(创建存储时)进行比较。
我不知道为什么你不能只将文件未添加到索引中并运行
git format-patch 'stash@{0}^1'..'stash@{0}'
这看起来是一样的。存储提交对象一定有一些特殊之处。无论如何,添加到索引将记录存储的第二个父级(索引提交)中的更改,从而绕过存储提交的问题。
其他注意事项:
对于合并提交,
git format-patch -1
将始终为空(存储是合并提交的特殊情况)。我不完全确定这是为什么,但请参阅 Git:如何为合并创建补丁? 了解更多详细信息。I can think of 2 ways.
Solution 1: Create a branch from the stash. This kind of defeats the original purpose of the stash feature, which was to avoid having to create the separate branch.
Solution 2: Add all changes to tracked files to the index before stashing. After stashing, run
which compares HEAD with the index (at the time the stash was created).
I'm not sure why you can't just leave files unadded to the index and run
git format-patch 'stash@{0}^1'..'stash@{0}'
which seems like the same thing. There must be something special about the stash commit object. In any case, adding to the index will record the changes in the stash's 2nd parent (the index commit), bypassing the issues with the stash commit.
Misc notes:
git format-patch -1
will always be blank for a merge commit (stashes are a special case of a merge commit). I'm not entirely sure why this is, but see Git: How to create patches for a merge? for more detail.如果您的文件是使用 git stash -u 隐藏的,则 git stash show -p 不起作用
If your files are stashed using
git stash -u
thengit stash show -p
doesn't work