Marc Rufer bio photo

Marc Rufer

Software engineer
@d-fens GmbH

Twitter Google+ LinkedIn Github Stackoverflow
RSS Feed

Apply Commit from one Repository to another

Marc Rufer 20 Feb 2016 Git

Imagine you have several repositories holding more or less the same code base that only differ in some customer specific functionality. At d-fens we had exactly the before described situation. We made some changes (i.e. new features or bugfixes) to one of the repositories and wanted to apply the changes to the second repo. One possibility is to apply all changes made to the first repo manually to the second repository by copying and pasting all the stuff from the first to the second repository. But we found an easier more consistent way to do that by using git format-patch and git-am commands.

Applying the last two commits from repo1 to repo2

The command below will create two patch files for the last two commits from HEAD of repo1 in the directory /path/to/repo2.

/path/to/repo2 $ git --git-dir=/path/to/repo1/.git format-patch -2 HEAD --stdout

To apply them the following commands have to be executed

/path/to/repo2 $ git am /path/to/1/0001-…-….patch
/path/to/repo2 $ git am /path/to/1/0002-…-….patch

If there are conflicts the patches cannot be applied without manual work. In such a case revert the patching by executing git am --abort and execute the git am command again but this time with the --reject switch.

/path/to/repo2 $ git am /path/to/1/000x-…-….patch --reject

When using the reject switch all the changes contained in a patch that could be applied without conflicts will be applied. For every conflicting part a file with ending .rej will be created. Now you have to manually apply the changes described in the .rej files to your repository (repo2). After you applied all the changes add all files to the index by using git add command and the you can continue with git am --continue.

comments powered by Disqus

Posts

HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task BUG "Task Scheduler service is not available" Error, if specifying Network Connection Condition HOWTO Sync OneDrive on Server even if Windows User not logged in HOWTO Access Microsoft Access Database with PowerShell Get NetworkCredential from PSCredential splits Username into Username and Domain HOWTO Create LocalDB File (.mdf) manually in Visual Studio 2015 HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient HOWTO Set up PowerShell Remote Session Configuration HOWTO Analyze PowerShell Scripts with PSScriptAnalyzer HOWTO Sign PDF with SuisseID NoBrainer PUT/PATCH/MERGE with OData Service Client Apply Commit from one Repository to another GitHub Repository creation with PowerShell HOWTO Sign PowerShell Script with SuisseID Swissunihockey game schedule PDF generator HOWTO Maven Release on JetBrains TeamCity Limitations when running Activiti in H2 Embedded Mode HOWTO Pylint Integration in IntelliJ 14 HOWTO Install Windows 10 IoT Core on Raspberry PI 2 from a VM First steps with Dropwizard Allman code style for IntelliJ First steps with Flyway IntelliJ - Setup custom inspection profiles Configuration properties meta-data support in IntelliJ AMQP Integration with spring integration OCA Java SE 7 Programmer I Certification Guide Swissunihockey key matcher Swisscom SMS-API-Client Some hints concerning logging with Log4j IFTTT A brilliant service Run deltacloud on a VirtualBox-VM Project specific maven settings in IntelliJ AngularJS Javascript for beginners Liferay in Action