git format-patch相对于git diff更便于操作,是更新的打包方式,应该采取这种打包方式。git diff打包的patch只能使用git apply处理。而git format-patch的补丁,可以应当使用git am命令。
基本用法
git format-patch xxxx.patch
format-patch可以基于分支进行打包,也可以基于上几次更新内容打包。
基于上几次内容打包
git format-patch HEAD^ 有几个^就会打几个patch,从最近一次打起
git format-patch HEAD^^ 最近的二个patch内容
以下代码作用同上
git format-patch -1
git format-patch -2
git format-patch -1 -4 //可以打包版本2,3的patch。但是发现有时候会把最近4个包都打包出来,具体原因未知。
参考:http://leave001.blog.163.com/blog/static/16269129320126944238969/
关于分支,可以参考:http://www.cnblogs.com/y041039/articles/2411600.html
git format-patch -M origin/master
format-patch 命令依次创建补丁文件,并输出文件名。上面的 -M 选项允许 Git 检查是
否有对文件重命名的提交。我们来看看补丁文件的内容:
$ cat 0001-add-limit-to-log-function.patch
From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function
Limit log functionality to the first 20
---
lib/simplegit.rb |
2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 76f47bc..f9815f1 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -14,7 +14,7 @@ class SimpleGit
end
def log(treeish = 'master')
-
+
command("git log #{treeish}")
command("git log -n 20 #{treeish}")
end
def ls_tree(treeish = 'master')
--
1.6.2.rc1.20.g8c5b.dirty
$ git am 0001-limit-log-function.patch
Applying: add limit to log function
$ git am 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Patch failed at 0001.
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
$ (fix the file)
$ git add ticgit.gemspec
$ git am --resolved
Applying: seeing if this helps the gem
如果想让 Git 更智能地处理冲突,可以用 -3 选项进行三方合并。如果当前分支未包含该补丁的基础代码或其祖先,那么三方合并就会失败,所以该选项默认为关闭状态。一般来
$ git am -3 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
$ git am -3 -i mbox
Commit Body is:
--------------------------
seeing if this helps the gem
--------------------------
Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all