Creating patches from branches

Instead of counting the number of commits you need to make patches for, you can create the patches by specifying the target branch when running the format-patch command.

Getting ready

We'll use the same repository as in the previous example:

$ git clone https://github.com/dvaske/offline-sharing.git
$ cd offline-sharing

Create the master branch locally without checking out:

$ git branch master origin/master

Make sure we have develop checked out:

$ git checkout develop

How to do it...

We'll pretend that we have been working on the develop branch and have made some commits. Now, we need to format patches for all these commits so we can send them to the repository maintainer or carry them to another machine.

Let's see the commits on develop not on master:

$ git log --oneline master..develop
c131c8b Adds functionality to prime-test a range of numbers
274a7a8 Adds Makefile for easy building
88798da Move print functionality of is_prime

Now, instead of running git format-patch -3 to get patches made for these 3 commits, we'll tell Git to create patches for all the commits that are not on the master branch:

$ git format-patch -o not-on-master master
not-on-master/0001-Move-print-functionality-of-is_prime.patch
not-on-master/0002-Adds-Makefile-for-easy-building.patch
not-on-master/0003-Adds-functionality-to-prime-test-a-range-of-numbers.patch

How it works…

Git makes a list of commits from develop that are not on the master branch, much like we did before creating the patches, and makes patches for these. We can check the content of the folder not-on-master, which we specified as the output folder (-o) and verify that it contains the patches as expected:

$ ls -1 not-on-master
0001-Move-print-functionality-of-is_prime.patch
0002-Adds-Makefile-for-easy-building.patch
0003-Adds-functionality-to-prime-test-a-range-of-numbers.patch

There's more…

The git format-patch command has many options and besides the -<n> option to specify the number of commits in order to create patches for and the -o <dir> for the target directory, some useful options are as follows:

  • -s, --signoff: Adds a Signed-off-by line to the commit message in the patch file with the name of the committer. This is often required when mailing patches to the repository maintainers. This line is required for patches to be accepted when they are sent to the Linux kernel mailing list and the Git mailing list.
  • -n, --numbered: Numbers the patch in the subject line as [PATCH n/m].
  • --suffix=.<sfx>: Sets the suffix of the patch; it can be empty and does not have to start with a dot.
  • -q, --quiet: Suppresses the printing of patch filenames when generating patches.
  • --stdout: Prints all commits to the standard output instead of creating files.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset