Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active February 21, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/87852ced09d7918322c0 to your computer and use it in GitHub Desktop.
Save Integralist/87852ced09d7918322c0 to your computer and use it in GitHub Desktop.
Git Patch and Apply (see alternative patch generation and applying in reverse: https://gist.github.com/Integralist/13d9f5e8ec197e5e53c6)
# presumes you're on a non-master branch with a set of changes that diff from `master`
# generate single patch file from multiple commits
git format-patch master --stdout > foobar.patch
# view stats on the patch file
git apply --stat foobar.patch
# test the patch without applying it (zero errors means the patch can be applied cleanly)
git apply --check foobar.patch
# apply and 'signoff' the patch (this is a different approach to `git apply`)
#
# the new commit messages added to `master` will contain a “Signed-off-by” tag.
git am --signoff < foobar.patch
git format-patch <base_branch_name>

Typically you'll specify master, as you'll be executing this command from your feature branch.

It'll generate a file for each new commit.

Use the following (--stdout) to get all new commits into a single patch file:

git format-patch master --stdout > new-feature.patch

The patch can then be applied like so:

git checkout review-new-feature
cat new-feature.patch | git am

or if you received multiple files:

cat *.patch | git am

Now you can check the result:

git log --oneline

Note: this gist is a shortened summary of this article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment