如何创建一个git patch
来源:互联网 发布:太上老君 老子 知乎 编辑:程序博客网 时间:2024/06/03 11:07
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.
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:
In GitX it would look like this:
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!
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
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.
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.
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.
- 如何创建一个git patch
- 如何创建一个git patch
- Git 创建Patch
- 如何创建一个 Git 仓库
- 如何使用git 发patch
- 如何使用git 生成patch
- 如何使用git 生成patch
- 如何使用git 生成patch
- 如何使用git 生成patch
- 如何创建一个自己的git服务器
- 如何用git创建和应用patch
- 添加一个文件夹及一些文件如何使用git生成patch
- 添加一个文件夹及一些文件如何使用git生成patch
- 添加一个文件夹及一些文件如何使用git生成patch
- 如何使用git 生成patch 和打入patch
- 如何使用git 生成patch 和打入patch
- 如何利用git制作和提交patch
- 如何利用git制作和提交patch
- win7下QT5.6+OPENCV3.0+minGW32 的配置
- Jva实现银行系统的简写
- 如何创建一条可靠的实时数据流
- 查询方式的按键驱动程序
- Git版本库
- 如何创建一个git patch
- shell中的各种括号
- 【java并发】原子性操作类的使用
- 错误中学习--this.addEvents is not a function
- Debug之路-2
- Android 性能优化一
- 各版本JDK新特性
- Android开发请求网络方式详解
- 假如华为挥手告别 深圳也不必过分挽留