Git is designed to be a distributed version control system. You can actually
send a patch for someone to review without the need to have a centralized Git
repository using the
git format-patch feature. This is very useful when your
remote Git repository is down.
When you’re ready to send the changes, use
git format-patch [BASE_BRANCH_NAME]:
$ git format-patch master 0001-Update-build-matrix.patch 0002-Display-current-gemfile-when-run-bundle-update.patch
Since my feature branch is two commits ahead of
master, Git creates two files,
one for each commit. If you inspect the file, you will see the changes alongside
your commit message:
From b2df28155560c68772063df3b3250d811e66f35e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist <email@example.com> Date: Mon, 25 Jan 2016 18:18:38 -0500 Subject: [PATCH 1/2] Update build matrix Build Appraisal against these versions of Ruby ... snip ... --- .travis.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6b56084..52d0bff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,12 +3,12 @@ sudo: false before_install: gem install bundler rvm: - - 1.8 - - 1.9 - - 2.0 - - 2.1 ... snip ... -- 2.7.0
If you want to create just one file, to more easily use as an attachment or
upload somewhere else, you can use the
--stdout option and redirect the output
to a file:
$ git format-patch master --stdout > new-feature.patch $ cat new-feature.patch From b2df28155560c68772063df3b3250d811e66f35e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist <firstname.lastname@example.org> Date: Mon, 25 Jan 2016 18:18:38 -0500 Subject: [PATCH 1/2] Update build matrix Build Appraisal against these versions of Ruby ... snip ... -- 2.7.0 From 54c7c4b5df552ef7540d4612c17dbbcc1079f47c Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist <email@example.com> Date: Mon, 25 Jan 2016 18:14:06 -0500 Subject: [PATCH 2/2] Display current gemfile when run `bundle update` This is done by setting `gemfile` setting directly and not through `BUNDLE_GEMFILE` setting. --- ... snip ...
While it’s possible to use
git diff and pipe the changes to a file, I think
git format-patch is a better way because it includes the commit message
describing the changes you made.
When you receive a patch file from someone, you can easily apply the patch using
git am command:
# Checkout to a new branch $ git checkout review-new-feature # If you received the patch in a single patch file $ cat new-feature.patch | git am # If you received multiple patch files $ cat *.patch | git am # Check the result $ git log --oneline 6787572 Display current gemfile when run `bundle update` 928928c Update build matrix ...
Note that the SHA of the patch that you merge with
git am will not be the same
SHA. However, the commit message will be intact.
If you are just getting started using Git, be sure to check out our new book titled Goal-Oriented Git which is currently in beta. It will help you get up to speed on how to use Git on your day-to-day work. If you are already feeling confident, you can check out our Mastering Git trail on Upcase which should help you on your path towards becoming a Git expert. The first three exercises in the trail are free.