如何创建一个git patch

本文详细介绍了如何使用Git创建补丁文件,并将其正确应用于另一个仓库。包括创建补丁、验证补丁内容、应用补丁以及检查补丁应用情况等步骤。

Git is quite common nowadays and a lot of people are asking me how they can create a patch file. Creating a patch file with git is quite easy to do, you just need to see how it’s done a few times.

This article will show you how to create a patch from the last few commits in your repository. Next, I’ll also show you how you can correctly apply this patch to another repository. ~ Before you start

To make creating patches easier, there are some common git practices you should follow. It’s not necessary, but it will make your life easier.

If you fix a bug or create a new feature – do it in a separate branch!

Let’s say you want to create a patch for my imdb gem. You should clone my repository and create a new branch for the fix you have in mind. In this sample we’ll do an imaginary fix for empty posters.

git clone git://github.com/ariejan/imdb.git
cd imdb
git checkout -b fix_empty_poster

Now, in the new fix_empty_poster branch you can hack whatever you need to fix. Write tests, update code etc. etc.

When you’re satisfied with all you changes, it’s time to create your patch. FYI: I’m assuming you made a few commits in the fix_empty_poster branch and did not yet merge it back in to themaster branch.

Creating the patch

Okay, I’ve made some commits, here’s the git log for the fix_empty_poster branch:

git log --pretty=oneline -3
* ce30d1f - (fix_empty_poster) Added poster URL as part of cli output (7 minutes ago)
* 5998b80 - Added specs to test empty poster URL behaviour (12 minutes ago)
* aecb8cb - (REL-0.5.0, origin/master, origin/HEAD, master) Prepare release 0.5.0 (4 months ago)

In GitX it would look like this:

imdb_fix_empty_poster_01

Okay, now it’s time to go and make a patch! All we really want are the two latest commits, stuff them in a file and send them to someone to apply them. But, since we created a separate branch, we don’t have to worry about commits at all!

git format-patch master --stdout > fix_empty_poster.patch

This will create a new file fix_empty_poster.patch with all changes from the current (fix_empty_poster) against master. Normally, git would create a separate patch file for each commit, but that’s not what we want. All we need is a single patch file.

Now, you have a patch for the fix you wrote. Send it to the maintainer of the project …

Applying the patch

… who will apply the patch you just sent! But, before you do that, there are some other steps you should take.

First, take a look at what changes are in the patch. You can do this easily with git apply

git apply --stat fix_empty_poster.patch

Note that this command does not apply the patch, but only shows you the stats about what it’ll do. After peeking into the patch file with your favorite editor, you can see what the actual changes are.

Next, you’re interested in how troublesome the patch is going to be. Git allows you to test the patch before you actually apply it.

git apply --check fix_empty_poster.patch

If you don’t get any errors, the patch can be applied cleanly. Otherwise you may see what trouble you’ll run into. To apply the patch, I’ll use git am instead of git apply. The reason for this is thatgit am allows you to sign off an applied patch. This may be useful for later reference.

git am --signoff < fix_empty_poster.patch
Applying: Added specs to test empty poster URL behaviour
Applying: Added poster URL as part of cli output

Okay, patches were applied cleanly and you’re master branch has been updated. Of course, run your tests again to make sure nothing got borked.

In you git log, you’ll find that the commit messages contain a “Signed-off-by” tag. This tag will be read by Github and others to provide useful info about how the commit ended up in the code.

imdb_signed_off

Git 中生成 patch 文件可以帮助开发者将代码变更以文件形式共享,便于他人审查或在其他仓库中应用这些变更。以下是几种常见的生成 patch 文件的方法。 ### 使用 `git format-patch` 生成 patch 文件 `git format-patch` 是一种专门用于生成标准 patch 文件的命令。它可以根据提交历史生成一个或多个 patch 文件,每个文件对应一个提交。例如,要为最近的一次提交生成 patch 文件,可以使用以下命令: ```bash git format-patch -1 HEAD ``` 此命令会为当前分支的最新一次提交生成一个 patch 文件,文件名通常类似于 `0001-commit-message.patch`,其中包含了提交的哈希值和提交信息。如果需要为多个提交生成 patch 文件,只需调整参数 `-n`,其中 `n` 表示提交的数量。例如,为最近的三次提交生成 patch 文件,可以使用: ```bash git format-patch -3 HEAD ``` 这种方法非常适合于准备一系列补丁供他人审查或提交到邮件列表中 [^1]。 ### 使用 `git diff` 生成 patch 文件 除了 `git format-patch` 之外,还可以使用 `git diff` 命令来生成 patch 文件。这种方式适用于尚未提交的更改。假设已经对某个文件进行了修改,但还没有提交,可以通过以下命令将这些更改保存为 patch 文件: ```bash git diff > mychanges.patch ``` 这里 `mychanges.patch` 是输出文件的名字,可以根据实际情况更改。这种方法生成的 patch 文件不包含提交信息,仅包含文件的具体更改内容 [^2]。 ### 提交更改并生成带有签名的 patch 文件 在生成 patch 文件之前,如果已经将更改提交到了本地仓库,可以在提交时使用 `-s` 参数自动添加 `Signed-off-by` 行,这有助于追踪谁对补丁负责。提交更改的命令如下: ```bash git add . git commit -s -m "Your commit message" ``` 之后,可以使用 `git format-patch` 或 `git diff` 命令生成 patch 文件,具体取决于是否需要包含提交信息 [^3]。 ### 总结 - **`git format-patch`** 适合于从提交历史中生成包含完整提交信息的 patch 文件。 - **`git diff`** 适用于生成尚未提交的更改的 patch 文件,不包含提交信息。 - 在提交更改时使用 **`-s`** 参数可以自动添加 `Signed-off-by` 行,有助于责任追踪。 通过上述方法,可以根据不同的需求选择合适的方式来生成 Git patch 文件。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值