从命令行创建Git补丁

如果您不想执行常规的提交-请求-请求流程,Git修补程序是将更改应用于项目的一种简便方法。 补丁是包含等效的Git提交差异的文件。

您可以使用git diff创建修改后的工作状态补丁。 diff输出采用正确的补丁格式。

$> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
	modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

$> git diff > ~/important-changes.patch

这将创建一个简单的补丁文件,该补丁文件可以应用于其他仓库,并在工作目录中创建相同的文件更改:

$> git status
On branch master
nothing to commit, working tree clean

$> git apply ~/important-changes.patch

$> git status
On branch master
Changes not staged for commit:
  [...]
	modified:   hello.txt

也可以从包含提交元数据的提交中创建格式化的补丁程序。 这些补丁已创建为通过电子邮件发送。

您可以使用git format-patch <since>根据特定点或范围内的所有提交创建补丁。

$> git format-patch origin/master --output ~/
/home/sebastian/0001-changed-file.patch
/home/sebastian/0002-changed-file-again.patch

# or into a single file
$> git format-patch origin/master --stdout > ~/important-commits.patch

同样,可以使用git apply来应用这些更改。 现在,我们还可以使用git am来应用所有提交,包括它们的元数据:

$> git am ~/important-commits.patch
Applying: changed file
Applying: changed file again

这些修补程序对于您需要时不时地应用于代码库的本地解决方法很有帮助。

帖子的内容已从我的新闻通讯037中重新发布

翻译自: https://www.javacodegeeks.com/2020/02/create-git-patches-from-command-line.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值