Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a git patch from one repository to another?

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?

like image 643
James Wheare Avatar asked May 31 '09 11:05

James Wheare


People also ask

How do you apply a commit from a different repo?

By creating a commit patch In that case you can create a patch of multiple successive commits and apply it to your current one. This can be done in two ways. One, cloning the third party repo in some other directory. Two, fetching the required branch of the third party repo in some other branch of your repo.

How do you apply a patch in VS code?

Right-click the item in the Solution Explorer and select Subversion > Apply Patch. Open the patch file. TortoiseMerge runs to merge the changes from the patch file with your working copy.


1 Answers

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/' 
like image 77
vergenzt Avatar answered Oct 08 '22 11:10

vergenzt