One important workflow to attempt to standardize is the method in which an organization rolls out updates to its production webservers. If you’re making heavy use of Git (and GitHub) in your development environment, it only makes sense to bundle your updates into patch files that can safely be applied and rolled back by someone with no working knowledge of the source code.
To create a patch file out of a Git commit, you’ll first want to retrieve the commit ID that your patch will retrieve data from. The git log command below retrieves information regarding the last 3 commits made:
$ git log -3 commit b29d8f2a0e6ad5595330f62f60b978fbc5696bcb Author: Sully Syed <firstname.lastname@example.org> Date: Fri May 13 15:41:05 2011 -0400 The Feed Importer module will hook into Drupal's cron scheduler script and retrieve new featured articles automatically. commit 65abfeeea27311effb21a547af99b3427126601f Author: Sully Syed <email@example.com> Date: Fri May 13 15:36:01 2011 -0400 Add the Google logo image to the footer directory. commit 33764e5d9ccd0222ad3d38cc1e77eb8242fb120d Author: Sully Syed <firstname.lastname@example.org> Date: Wed May 11 15:45:41 2011 -0400 Fixed the typo that would have made Skynet sentient. $ git format-patch --help
The git format-patch command allows us to select a range of commit IDs to bundle into a patch. For our example, the range will consist of begin and end with the same commit ID to limit the patch to the one commit:
$ git format-patch -M -C b29d8f2a0e6ad5595330f62f60b978fbc5696bcb~1..b29d8f2a0e6ad5595330f62f60b978fbc5696bcb 0001-The-Feed-Importer-module-will-hook-into-Drupal-s-cron-.patch
The patch file created takes the name 0001-The-Feed-Importer-module-will-hook-into-the-Drupal-s-cron-.patch, which can then be e-mailed or sent by other means to the production staff who will apply it.
Reference: Blogging the Monkey: git format-patch for specific commit-ids
Would you mind explaining the meaning of repeating the commit hash twice, and the ~1.. in between them?
Why is it?
Instead of just
Hi alphydan – the answer to your question is that the format-patch command in Git is designed to allow you to combine one or more commits into a single patch file, and that’s done by specifying a range. In this command…
…you’re actually stating, please create (“format”) a patch that contains all of the commits from one before b29d8f2a0e6ad5595330f62f60b978fbc5696bcb (this is what the ~1 denotes, “one before”) to the commit b29d8f2a0e6ad5595330f62f60b978fbc5696bcb.
It just so happens that we only have one patch in that range, which makes the command appear to be unnecessarily long for no reason.
You can also save the patch to the clipboard instead of to a file. Patch files are applied to your working copy.
Pingback: Create A Patch File Using Git – How ot be in 2016
Very useful – thanks a lot