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 the master 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 -
* ce30d1f - (fix_empty_poster) Added poster URL as part of cli output ( minutes ago)
* 5998b80 - Added specs to test empty poster URL behaviour ( minutes ago)
* aecb8cb - (REL-0.5., origin/master, origin/HEAD, master) Prepare release 0.5. ( months ago)

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!

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 that git 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.

From : https://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/

How to create and apply a patch with Git的更多相关文章

  1. linux git patch 和patch以及git diff 命令

    1.git log 查看commit id,修改前为id1,修改后id2 2.根据id1到id2有几次提交来生成几个patch,否则的话会根据所有节点生成很多patch 比如: commit id2 ...

  2. How to Create an OCM Response file to Apply a Patch in Silent Mode - opatch silent

    Windows Server 2019 打补丁时缺少Oracle Configuration Manager(OCM) 响应文件处理方式. 适用: Oracle Universal Installer ...

  3. 如何使用git 生成patch 和打入patch【转】

    本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ...

  4. 如何使用git 生成patch 和打入patch 标签: gitpatch【转】

    本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ...

  5. Launch VINS example (Euroc dataset) in RTAB-MAP

    $ roslaunch rtabmap_ros euroc_datasets.launch args:="-d RGBD/CreateOccupancyGrid false Odom/Str ...

  6. 巧用svn create patch(打补丁)方案解决定制版需求

    最近项目定制版越来越多,维护,同步代码非常费事.以前的思路如下图: 以前的svn目录结构如下图: 这样问题有2个: 若在一个定制包中修复了其他定制包也有的bug,同步更新其他包的代码时,非常费劲+机械 ...

  7. git patch 使用

    使用git的时候,需要删除几个id,会对到之前的代码,但又想保留现在的代码,以便后面从新合并,所以就将现在的代码打包成patch,留到下次合并. 参考链接 http://www.jianshu.com ...

  8. linux 操作

    正在运行的内核和系统信息 # uname -a # 获取内核版本(和BSD版本) # lsb_release -a # 显示任何 LSB 发行版版本信息 # cat /etc/SuSE-release ...

  9. (一)ROS系统入门 Getting Started with ROS 以Kinetic为主更新 附课件PPT

    ROS机器人程序设计(原书第2版)补充资料 教案1 ROS Kinetic系统入门 ROS Kinetic在Ubuntu 16.04.01 安装可参考:http://blog.csdn.net/zha ...

随机推荐

  1. 【转】超实用PHP函数总结整理

    原文链接:http://www.codeceo.com/article/php-function.html 1.PHP加密解密 PHP加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过 ...

  2. synchronized的实现原理-java并发编程的艺术读书笔记

    1.synchronized实现同步的基础 Java中的每个对象都是可以作为锁,具体有3种表现. 1.对于普通同步方法,锁是当前实例对象. 2.对于静态同步方法,锁是当前类的Class对象. 3.对于 ...

  3. ImageLightbox.js – 响应式的图片 Lightbox 插件

    ImageLightbox.js 是一款很简洁的用于显示图片灯箱效果(Lightbox)的插件,没有字幕,导航按钮或默认背景.如果默认功能不够用的话,你可以很容易地自定义 JavaScript 函数扩 ...

  4. 【再探backbone 01】模型-Model

    前言 点保存时候不注意发出来了,有需要的朋友将就看吧,还在更新...... 几个月前学习了一下backbone,这段时间也用了下,感觉之前对backbone的学习很是基础,前几天有个园友问我如何将路由 ...

  5. CodeIgniter_2 路由中定义伪静态 直接映射到相关的控制器

    某些情况下 隐藏路径信息 使用伪静态定义: RewriteRule ^no/torrent(.*)$ /index.php/torrent/doit/$1 CodeIgniter会自动识别到 no 控 ...

  6. Sublime Text 3汉化中文版

    Sublime Text 3汉化中文版是Sublime Text2的升级版.Sublime Text 是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Linux,Windows ...

  7. java文件读写操作大全

    转自:http://hi.baidu.com/0_net/blog/item/8566fc2bb730c293033bf63e.html一.获得控制台用户输入的信息 public String get ...

  8. Javascript - Arraylike的7种实现

    jQuery的崛起让ArrayLike(类数组)在javascript中大放异彩,它的出现为一组数据的行为(函数)扩展提供了基础. 类数组和数组相似,具有数组的某些行为,但是它相比数组可以更加自由的扩 ...

  9. Hibernate3注解[转]

    Hibernate3注解 收藏 1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="&qu ...

  10. Objective-C 快速入门--基础(二)

    1.什么是继承?OC中的继承有哪些特点? “继承”是面向对象软件技术当中的一个概念.如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类&quo ...