How to create and apply a patch with Git
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的更多相关文章
- linux git patch 和patch以及git diff 命令
1.git log 查看commit id,修改前为id1,修改后id2 2.根据id1到id2有几次提交来生成几个patch,否则的话会根据所有节点生成很多patch 比如: commit id2 ...
- 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 ...
- 如何使用git 生成patch 和打入patch【转】
本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ...
- 如何使用git 生成patch 和打入patch 标签: gitpatch【转】
本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ...
- Launch VINS example (Euroc dataset) in RTAB-MAP
$ roslaunch rtabmap_ros euroc_datasets.launch args:="-d RGBD/CreateOccupancyGrid false Odom/Str ...
- 巧用svn create patch(打补丁)方案解决定制版需求
最近项目定制版越来越多,维护,同步代码非常费事.以前的思路如下图: 以前的svn目录结构如下图: 这样问题有2个: 若在一个定制包中修复了其他定制包也有的bug,同步更新其他包的代码时,非常费劲+机械 ...
- git patch 使用
使用git的时候,需要删除几个id,会对到之前的代码,但又想保留现在的代码,以便后面从新合并,所以就将现在的代码打包成patch,留到下次合并. 参考链接 http://www.jianshu.com ...
- linux 操作
正在运行的内核和系统信息 # uname -a # 获取内核版本(和BSD版本) # lsb_release -a # 显示任何 LSB 发行版版本信息 # cat /etc/SuSE-release ...
- (一)ROS系统入门 Getting Started with ROS 以Kinetic为主更新 附课件PPT
ROS机器人程序设计(原书第2版)补充资料 教案1 ROS Kinetic系统入门 ROS Kinetic在Ubuntu 16.04.01 安装可参考:http://blog.csdn.net/zha ...
随机推荐
- Lucene.net站内搜索—3、最简单搜索引擎代码
目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...
- [Android] WebView内的本地网页,使用XMLHttpRequest读取本地档案
[Android] WebView内的本地网页,使用XMLHttpRequest读取本地档案 问题情景 在Android里,可以使用WebView来呈现本地或是远程的网页内容.但是在显示本地网页时,如 ...
- 12款响应式的 jQuery 旋转木马(传送带)插件
在企业网站,作品集网站,电子商务网站或任何其他类型的网站内容显示图片可以使用 jQuery 旋转木马(传送带)插件来实现. jQuery 旋转木马插件允许开发人员以水平或垂直的方式显示内容,视频和图像 ...
- 使用 HTML5 Canvas 绘制出惊艳的水滴效果
HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ...
- jQuery.swatches – 把 Div 变成可爱的调色板
jQuery.swatches 是一款开源的 jQuery 插件,能够把一个 Div 转换成漂亮的调色板.您可以自定义你想要的类,使用不同的类可以生成不同的调色板.这个功能能够帮助设计师方便的挑选设计 ...
- 微信小程序之后台https域名绑定以及免费的https证书申请
微信小程序在11月3号发布了,这是一个全新的生态,没有赶上微信公众号红利的开发者,运营者可别错过这趟车了. 但是微信的后台需要全https,之前我还不相信,后台注册了后进后台才发现,服务器配置如下图 ...
- Ubuntu 16.04 LTS 安装配置 Nginx 1.10.0 Php7.0-FPM
1. 安装Nginx,Php-7.0 ~$ sudo add-apt-repository ppa:nginx/stable ~$ sudo apt-get update ~$ sudo apt-ge ...
- app让个别界面横屏,其他的为竖屏,解决如下
app让个别界面横屏,其他的为竖屏,解决如下 APP设置里面,一定要设置可以旋转的方向 appdelegate里面重新系统方向代理 func application(application: UIAp ...
- eclipse出现感叹号的解决办法
当eclipse导入项目出现红叉但无提示错误时,去看:1>菜单路径----Window/Show View/Console2>菜单路径----Window/Show View/Error ...
- GCD中的dispatch_group函数的详解
<一>引入dispatch_group函数的目的 在追加到dispatch_Queue中的多个处理全部结束后想要执行结束的处理,这种需求经常会在我们的程序中出现 (第一种情况)只使用一个S ...