为什么 git format-patch 不适用于存储?

发布于 2024-10-10 18:47:56 字数 128 浏览 2 评论 0原文

如果我运行

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫相离 2024-10-17 18:47:56

这似乎是因为存储提交被表示为合并(在其父级和当时的索引状态之间),而合并提交上的 format-patch 不执行任何操作。

如果你

git format-patch stash@{0}{,^}

这么说,它会在藏品和每个父母之间吐出补丁。

为了便于说明,藏匿处如下所示:

*   99aedb8 (refs/stash) WIP on master: 668ff36 initial commit
|\  
| * 6b8d77f index on master: 668ff36 initial commit
|/  
* 668ff36 (HEAD, master) initial commit

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

git format-patch stash@{0}{,^}

then it will spit out patches between the stash and each parent.

For illustration, this is what the stash looks like:

*   99aedb8 (refs/stash) WIP on master: 668ff36 initial commit
|\  
| * 6b8d77f index on master: 668ff36 initial commit
|/  
* 668ff36 (HEAD, master) initial commit
水水月牙 2024-10-17 18:47:56

您可以尝试

git stash show -p > ~/Desktop/stash.patch

这将在您的桌面上生成最新补丁和原始父补丁的补丁文件。

在 show 选项下的 git-stash 文档 中进行了描述

You could try

git stash show -p > ~/Desktop/stash.patch

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

絕版丫頭 2024-10-17 18:47:56

我可以想到两种方法。

解决方案 1:从存储中创建一个分支。这违背了存储功能的最初目的,即避免创建单独的分支。

解决方案 2:在存储之前将对跟踪文件的所有更改添加到索引中。存储后,运行

git format-patch 'stash@{0}^1'..'stash@{0}^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

git format-patch 'stash@{0}^1'..'stash@{0}^2'

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.

薄情伤 2024-10-17 18:47:56

如果您的文件是使用 git stash -u 隐藏的,则 git stash show -p 不起作用

If your files are stashed using git stash -u then git stash show -p doesn't work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文