git 导入补丁_用git导出和导入补丁

git 导入补丁

Most of us that use git probably only have use GitHub -- thus is the popularity of their service.  If you (or a parent project) don't use a service like GitHub, however, you'll need to export patches for review.  Let's have a look at how to export a patch with git!

我们大多数使用git的人可能只使用GitHub-因此,他们的服务非常受欢迎。 但是,如果您(或父项目)不使用GitHub之类的服务,则需要导出补丁进行审核。 让我们看看如何使用git导出补丁!

导出补丁 (Exporting a Patch)

Let's say you've created a feature branch for your impending changeset, made changes, and committed those changes.  Now it's time to export your commits to a patch file -- you would execute the following:

假设您已经为即将发生的变更集创建了一个功能分支,进行了变更并提交了这些变更。 现在是时候将提交导出到补丁文件了,您可以执行以下操作:


git format-patch master --stdout > my-patch-file.patch


The command above outputs your commits to a .patch file.  This patch file can be imported into other repositories for testing, application, etc.  Some repositories require the most detailed patch output.  You can provide that with:

上面的命令将您的提交输出到.patch文件。 可以将此修补程序文件导入其他存储库以进行测试,应用程序等。某些存储库需要最详细的修补程序输出。 您可以提供:


git format-patch master --full-index --stdout > my-patch-file.patch


From the git documentation, --full-index signifies: Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

在git文档中,-- --full-index表示: 生成补丁格式输出时,在“索引”行上显示完整的图像前和图像后Blob对象名称,而不是前几个字符。

导入补丁 (Importing a Patch)

If you receive a patch file, you'll want to do a few checks before trying to merge it!

如果收到补丁文件,则在合并之前需要进行一些检查!

确保补丁相关性 (Ensure Patch Relevance)

You can ensure the patch applies to the current set of work:

您可以确保补丁程序适用于当前的工作集:


# See if patch is applicable
git apply --check my-patch-file.patch

# Ensure patch applies to current index
git apply --index my-patch-file.patch


查看补丁差异信息 (View Patch Diff Information)

If you want to know which files have been changed, added, or removed, you can use the following command:

如果您想知道哪些文件已被更改,添加或删除,则可以使用以下命令:


# See which files have been changed
git apply --stat my-patch-file.patch


合并代码 (Merge the Code)

Once you're satisfied with the patch code and want to merge and test (testing should be done on a feature branch), you can execute:

对补丁代码感到满意并想要合并和测试(测试应该在功能分支上完成)后,您可以执行:


# Signs the patch by merger for history
git am --signoff my-patch-file.patch


Welcome to some of the operations that GitHub (and likewise services) do for us in the background.  I love doing stuff from command line but I'd much rather use  an elegant front-end for this type of stuff.  In the case you're stuck without a UI, however, keep these commands handy!

欢迎使用GitHub(以及类似服务)在后台为我们执行的一些操作。 我喜欢从命令行执行操作,但是我宁愿使用优雅的前端来处理此类操作。 在没有UI的情况下,请保持这些命令的方便!

翻译自: https://davidwalsh.name/git-export-patch

git 导入补丁