如何创建一个git patch

转载 2016年06月01日 08:57:53

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 上多次commit 合并成一个 patch

对于一个test 工程   git log 看其提交的过程: commit 9fdd76eb5fd06b1b7f2af6800dc13e51099c40d9 Author: ShichaoXu ...
  • GZgengzhen
  • GZgengzhen
  • 2016年12月15日 14:04
  • 2570

如何创建一个git patch

Git is quite common nowadays and a lot of people are asking me how they can create a patch file. Cre...
  • hellochenlian
  • hellochenlian
  • 2015年01月09日 18:13
  • 538

如何使用git 生成patch 和打入patch

平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用git 生成patch 给到我们,那我们就需要把客户给到patch 打入到我们的project ,基于这样一个场景,我把git ...
  • liuhaomatou
  • liuhaomatou
  • 2017年01月13日 17:36
  • 12524

多个commit 合并为一个patch

有时候开发一个功能需要commit 很多次才能有一个比较精简的版本。  但是此时只允许打一个patch,如果这样就要把这几个commit 信息合并。 可以通过git reset + git mer...
  • XscKernel
  • XscKernel
  • 2013年12月31日 18:04
  • 6357

git 生成patch和使用patch

啰嗦下:发现自己的blog被转载了,不过没有注明出处,有点痛并快乐着的感觉。这里正式申明下:转载请注明出处,感谢。如有商用目的请务必知会本人。           在android大环境下工作,迟...
  • ts1122
  • ts1122
  • 2013年03月21日 23:46
  • 81335

git 回退某个版本或者去掉某些patch

diff -urN linux-x.y.z/    linux/     >   xxx_patch patch -p1 diffstat -p1 xxx_patch git checkout  ....
  • xiaolubk
  • xiaolubk
  • 2015年09月11日 10:13
  • 2069

Git Patch使用: 生成,导入,解决冲突

1. Git Patch git patch包括2中: 标准diff 和git format-patch 2. 生成patch 2.1 git diff 生成标准patch 最简单, 但是很多...
  • xiyanlgu
  • xiyanlgu
  • 2015年05月20日 14:48
  • 3176

如何打patch 及git 打patch 失败的处理方法

最近分配了一些打patch的任务。打的那个叫稀巴烂。想必对我很失望。这个过程中学到了如何打patch. 正常来说我们打的git生成的标准patch如果没有没有问题那很好打。 只需要进入patch对应的...
  • RonnyJiang
  • RonnyJiang
  • 2017年01月11日 15:46
  • 2906

Git 合并 patch 时的冲突处理一例

from : http://www.fwolf.com/blog/post/448 git version 1.6.0.4 合并 patch 的时候,如果生成 patch 的“原稿”找不到,...
  • Fybon
  • Fybon
  • 2016年03月01日 18:28
  • 1374

Git patch生成以及更新的命令

1 使用git format-patch生成所需要的patch: 当前分支所有超前master的提交: git format-patch -M master 某次提交以后的所有patch: g...
  • daydring
  • daydring
  • 2015年01月13日 15:10
  • 10384
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何创建一个git patch
举报原因:
原因补充:

(最多只允许输入30个字)