Formatted patches

You can apply commits from another branch to the current branch with no problem.

  • For a single commit, it's a cherry-pick

  • For a sereis of commit, it's a rebase

However, this needs to be in the same repo. What if you want to apply a commit to a different repo?

How to do it

Well, you can do that via formatted patches. The way it works is that you

  1. save the commit as a formatted patch, a plain text file

  2. send the file to someone

  3. execute the formatted patch

Command

To generate the formatted patch

git format-patch -l <commit> # Single commit
git format-patch <commit-1>...<commit-2> # A series of commits

How it works

Each commit consists of a set of changes, and each change can be expressed as an instruction. A formatted patch is a text version of these instructions, which Git can understand and run.

What to look out for

To apply the formatted patch, the current file should be compatible. For example, if the change is to remove the line import pandas as pd in file analyse.py, it will fail if

  • you don't have a file named analyse.py, or

  • the file doens't have this line

Last updated