如何将git补丁从一个存储库应用到另一个存储库? [英] How to apply a git patch from one repository to another?

查看:125
本文介绍了如何将git补丁从一个存储库应用到另一个存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个存储库,一个是库的主要库,另一个是使用该库的项目。



如果我修复in我想要一个简单的方法将补丁应用到上游。



文件的位置在每个存储库中都是不同的。




  • 主要回购: www.playdar.org/static/playdar.js

  • 项目: playlick.com/lib/playdar.js



我试过使用 git format-patch - lib / playdar.js ,然后在playdar主repo上 git am ,但修补程序文件中的不同文件位置引发了错误。



有没有一种简单的方法可以将补丁从给定文件上的给定提交应用到其他任意文件?

点,如果你想要应用补丁的文件不在git仓库中,该怎么办?

解决方案

如果手动编辑补丁文件不合理或不可行,可以使用标准选项完成( git apply git format-patch 和GNU patch )。


  1. -p 移除 n 修补程序路径中的主要目录。


  2. 处理 -p 后, - directory =< root> prepends root 在应用之前添加到修补程序中的每个路径。




示例



所以,举个例子,它最初位于 static / playdar.js 并将其应用于 lib / playdar.js ,您可以运行:

  $ cat patch_file | git am \ 
-p1 \#删除1个主目录('static /')
--directory ='lib /'#prepend'lib /'


I have two repositories, one is the main repo for a library, and the other is a project using that library.

If I make a fix to the in the subservient project, I'd like an easy way to apply that patch back upstream.

The file's location is different in each repository.

  • Main repo: www.playdar.org/static/playdar.js
  • Project: playlick.com/lib/playdar.js

I tried using git format-patch -- lib/playdar.js on the playlick project, and then git am on the main playdar repo, but the differing file locations in the patch file raised an error.

Is there an easy way to apply the patch from a given commit on a given file to another arbitrary file elsewhere?

For bonus points, what if the file you want to apply the patch to isn't in a git repository?

解决方案

If manually editing the patch file is out of the question or infeasible, this can be done with standard options (available in git apply, git format-patch and GNU patch).

  1. -p<n> removes n leading directories from the paths in the patch.

  2. After processing -p, --directory=<root> prepends root to each of the paths in the patch before applying.

Example

So, for your example, to take a patch that was originally on static/playdar.js and apply it to lib/playdar.js, you would run:

$ cat patch_file | git am     \ 
          -p1                 \ # remove 1 leading directory ('static/')
         --directory='lib/'     # prepend 'lib/'

这篇关于如何将git补丁从一个存储库应用到另一个存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆