如何创建一个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 patch

Git is quite common nowadays and a lot of people are asking me how they can create a patch file. Cre...

Git 上多次commit 合并成一个 patch

对于一个test 工程   git log 看其提交的过程: commit 9fdd76eb5fd06b1b7f2af6800dc13e51099c40d9 Author: ShichaoXu ...

Git patch生成以及更新的命令

1 使用git format-patch生成所需要的patch: 当前分支所有超前master的提交: git format-patch -M master 某次提交以后的所有patch: g...
  • daydring
  • daydring
  • 2015年01月13日 15:10
  • 10041

如何利用git制作和提交patch

UNIX世界的软件开发大多都是协作式的,因此,Patch(补丁)是一个相当重要的东西,因为几乎所有的大型UNIX项目的普通贡献者,都是通过 Patch来提交代码的。作为最重要的开源项目之一,Linux...

Git导出差分(diff)包--before/after/patch

通过shell脚本并调用git生成不同分支之间的差分包。

git的patch功能调研

之前在英特尔实习的时候才接触过patch这个概念,在Linux kernel的贡献者都是通过patch提交代码的,各个开发者从代码库clone代码后,将修改的代码以patch的方式email给维护人员...

转: git patch

打patch: git-format-patch x..y x y 是哈希值,从x到y依次打patch 如果只是生成一个commit的patch,可以用git-format-patch -1 c...

svn 及 git patch 文件

项目开发中经常会遇到A同事编写的代码需要B同事review,然后由C同事提交,patch文件是一种很好的代码传递方式。 1  svn patch文件操作: 1)      生成svn patch文件 ...

Git的Patch功能(转)

http://www.cnblogs.com/y041039/articles/2411600.html UNIX世界的软件开发大多都是协作式的,因此,Patch(补丁)是一个相当重要的...
  • mdj_bj
  • mdj_bj
  • 2012年08月03日 13:52
  • 280

Git am PATCH冲突解决

本文通过实例记录使用`git am PATCH`打patch过程中遇到的一次冲突问题和解决过程。使用本文中的方法可以保留patch中commit信息,比较推荐。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何创建一个git patch
举报原因:
原因补充:

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