Question

I modify a file in different commits in a repo A, while I keep a another repo B which almost have the same files.

I want to generate some patches from repo A which modify some file and apply them onto repo B.

How can I do that?

Was it helpful?

Solution

Answering your main question: If you want to do this using patches, on repo A:

git format-patch <starting hash>..<ending hash>

Then, on repo B:

git apply <each patch file generated using the above method>

I'd caution you to make sure the files are very similar, though. Patches can be a pain to apply correctly if the repositories have diverged much.

Second approach: Depending on how similar the repositories are, it may be useful for you to add repo A as a secondary remote for repo B. This would allow you to fetch and cherry-pick commits as you do with any other upstream repository and avoid patch files altogether.

This would look like:

git remote add repoA <path to repo A>
git fetch repoA
git cherry-pick <relevant commits in repo A>

This:

  • Adds Repo A as a second remote.
  • Fetches Repo A's commit history.
  • Cherry-picks the relevant commits to your current repository.

That said, there is a lot of unnecessary overhead in adding a second remote, such as downloading a lot of unnecessary history, so you may find that patches suit your problem better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top