« Back to Index

Git Patch and Apply (see alternative patch generation and applying in reverse: https://gist.github.com/Integralist/13d9f5e8ec197e5e53c6)

View original Gist on GitHub

1. shorter version.bash

# 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 Patch and Apply.md

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