简体   繁体   English

使用 git diff 生成补丁文件

[英]Using git diff to generate the patch file

I got some problems to understand why the patch files generated by git diff are different.我在理解为什么git diff生成的补丁文件不同时遇到了一些问题。

Below is my git log: the local_tracking_branch has one extra feature which not been push to remote.下面是我的 git 日志: local_tracking_branch有一个额外的功能没有推送到远程。

commit COMMIT_HASH_1 (HEAD -> local_tracking_branch)

commit COMMIT_HASH_2 (origin/mainline, origin/HEAD)

I use git diff mainline > patch1 to generate the first patch.我使用git diff mainline > patch1来生成第一个补丁。

Then I use git diff COMMIT_HASH_1 COMMIT_HASH_2 > patch2 to generate the second patch.然后我使用git diff COMMIT_HASH_1 COMMIT_HASH_2 > patch2来生成第二个补丁。

It turns out that patch1 is larger than patch2 about 10K bytes.事实证明, patch1大于patch2约10K字节。

A more context here, I am instructed to use the first approach to generate the patch for release, but I am wondering it is ok to use second approach to generate the patch file.这里有更多的上下文,我被指示使用第一种方法来生成发布补丁,但我想知道使用第二种方法生成补丁文件是否可以。

A another scenario is that once I push the feature into remote mainline, is it ok that I could use second approach git diff commmit-hash1 commit-hash2 to generate the valid patch?另一种情况是,一旦我将该功能推送到远程主线,我可以使用第二种方法git diff commmit-hash1 commit-hash2来生成有效补丁吗?

Thank for the explanation.谢谢你的解释。

I use git diff mainline我使用git diff mainline

This form of git diff compares the given commit—the hash ID will be the result of running:这种形式的git diff比较给定的提交——哈希 ID 将是运行的结果:

git rev-parse mainline

which may well be quite different from that of:这可能与以下内容完全不同:

git rev-parse origin/mainline

—to the current work-tree. ——到当前的工作树。 More precisely, it compares the saved snapshot in the given commit, to the current work-tree (the files you can see).更准确地说,它将给定提交中保存的快照与当前工作树(您可以看到的文件)进行比较。

I use git diff COMMIT_HASH_1 COMMIT_HASH_2我使用git diff COMMIT_HASH_1 COMMIT_HASH_2

This form of git diff compares the saved snapshot in the first commit to that in the second.这种形式的git diff将第一次提交中保存的快照与第二次提交中的保存快照进行比较。

Given these fragments of output from git log :鉴于git log这些输出片段:

 commit COMMIT_HASH_1 (HEAD -> local_tracking_branch) commit COMMIT_HASH_2 (origin/mainline, origin/HEAD)

we can predict that mainline resolves to a third commit hash.我们可以预测mainline解析为第三个提交哈希。 We cannot say for sure whether your current work-tree matches the saved snapshot in COMMIT_HASH_1 , but if git status says that everything is "clean" and there is nothing to commit, it probably does match that.我们无法确定您当前的工作树是否与COMMIT_HASH_1保存的快COMMIT_HASH_1匹配,但是如果git status说一切都是“干净的”并且没有什么可提交的,它可能确实匹配。

So the most likely source of all the oddness is that mainline refers to some third commit whose snapshot is not much like the snapshot in the commit to which the name origin/mainline resolves.因此,所有奇怪的最可能来源是mainline指的是某个第三次提交,其快照与名称origin/mainline解析的提交中的快照不太相似。 (You already have that hash ID, but to see it again, by itself, use the earlier-mentioned git rev-parse origin/mainline .) But another possibility is that the current work-tree is different from any and/or all of these other commits. (您已经拥有该哈希 ID,但要再次查看它,请使用前面提到的git rev-parse origin/mainline 。)但另一种可能性是当前的工作树不同于任何和/或所有这些其他提交。 Since the work-tree consists of ordinary files, any of your ordinary (non-Git) commands can make any arbitrary changes they like to these ordinary files.由于工作树由普通文件组成,您的任何普通(非 Git)命令都可以这些普通文件进行任何他们喜欢的任意更改。

A another scenario is that once I push the feature into remote mainline, is it ok that I could use second approach git diff commmit-hash1 commit-hash2 to generate the valid patch?另一种情况是,一旦我将该功能推送到远程主线,我可以使用第二种方法git diff commmit-hash1 commit-hash2来生成有效补丁吗?

You can do this any time you like.您可以随时执行此操作。 How valid it is depends on who has which commit and what they want to do with those commits.它的有效性取决于谁进行了哪些提交以及他们想对这些提交做什么。

The thing to keep in mind is that names can change over time.要记住的是,名称会随着时间而改变。 A name like mainline or origin/mainline refers to some commit by hash ID.mainlineorigin/mainline这样的名称是指通过哈希 ID 进行的某些提交。 You can find out which one they name right now using git rev-parse .你可以使用git rev-parse找出他们现在命名的那个。 Tomorrow, after people do things with branches, they may refer to different commit hash IDs.明天,人们用分支做事后,可能会参考不同的提交哈希ID。

The hash IDs, however, remain fixed forever.然而,散列 ID 永远保持固定。 Given any particular hash ID, that hash ID represents that commit.给定任何特定的哈希 ID,该哈希 ID 表示提交。 No other commit can ever have that hash ID.没有其他承诺不能哈希ID。 The snapshot for that commit is frozen for all time;该提交的快照始终处于冻结状态; that hash ID will always represent those files, in that form.该哈希 ID 将始终以该形式表示这些文件。

Hash IDs are long and ugly and random-looking and very hard to type in. That's one reason we use names.哈希 ID 又长又丑,看起来很随意,很难输入。这就是我们使用名称的原因之一。 Branch names, however, change over time.然而,分支名称会随着时间而改变。 That's one reason we use tag names: tag names are designed to be stable, to not -change over time.这就是我们使用标签名称的原因之一:标签名称被设计为稳定的,不会随着时间的推移改变。 Use a commit hash;使用提交哈希; or, use a tag name to specify one particular commit, and don't move tag names around manually (Git won't move them on its own) and you have a steady way to refer to that particular commit.或者,使用标签名称来指定一个特定的提交,并且不要手动移动标签名称(Git 不会自行移动它们),并且您有一种稳定的方式来引用该特定提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM