如何创建一个git patch

本文将指导您如何使用Git从仓库中创建补丁文件,并正确应用于另一个仓库。包括创建补丁文件的步骤,以及如何检查、测试和应用补丁文件,确保代码更新的顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. N-ext, 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补丁的基本方法 在Git中,创建和应用补丁是一项常见的操作,用于分享代码更改或将更改从一个分支移植到另一个分支。以下是关于如何创建和应用Git补丁的详细说明。 #### 创建补丁 创建Git补丁的过程相对简单,主要使用`git format-patch`命令。此命令会生成一系列补丁文件,每个文件对应一次提交[^1]。例如: ```bash git format-patch master --stdout > fixed_version.patch ``` 上述命令将当前分支相对于`master`分支的所有更改导出为一个名为`fixed_version.patch`的补丁文件。如果需要针对特定提交范围生成补丁,可以指定起始和结束提交的哈希值,例如: ```bash git format-patch <start-commit>..<end-commit> --stdout > specific_changes.patch ``` #### 检查补丁 在应用补丁之前,可以通过以下命令检查补丁是否能够无误地应用: ```bash git apply --stat fixed_version.patch git apply --check fixed_version.patch ``` `--stat`选项显示补丁将影响哪些文件及行数,而`--check`选项则验证补丁是否可以成功应用。如果没有错误输出,则表示补丁可以干净地应用[^3]。 #### 应用补丁 应用补丁时,可以选择使用`git apply`或`git am`命令。两者的区别在于`git am`不仅应用补丁,还会保留原始提交信息,并允许添加签名[^2]。例如: ```bash git am --signoff < fixed_version.patch ``` 该命令会自动应用补丁并提交更改,同时添加签名以记录应用者的信息。如果仅需应用更改而不涉及提交历史,可以使用以下命令: ```bash git apply fixed_version.patch ``` #### 提交更改 无论使用哪种方式应用补丁,都需要确保更改已正确提交。如果是通过`git am`应用补丁,则无需额外提交操作;如果是通过`git apply`应用补丁,则需要手动提交: ```bash git commit -m "Applied patch for fixed_version" ``` 或者直接使用`git commit`命令完成提交操作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值