Skip to content

Instantly share code, notes, and snippets.

@jplindgren
Forked from nepsilon/how-to-git-patch-diff.md
Last active July 1, 2022 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jplindgren/3e4ac109afc2348951b11bcb715df7db to your computer and use it in GitHub Desktop.
Save jplindgren/3e4ac109afc2348951b11bcb715df7db to your computer and use it in GitHub Desktop.
How to generate and apply patches with git? — First published in fullweb.io issue #33

How to generate and apply patches with git?

It sometimes happen you need change code on a machine from which you cannot push to the repo. You’re ready to copy/paste what diff outputs to your local working copy.

You think there must be a better way to proceed and you’re right. It’s a simple 2 steps process:

1. Generate the patch:

git diff > some-changes.patch

or

curl https://example.com/file-0.0.1.diff | git apply

2. Apply the diff:

Then copy this patch to your local machine, and apply it to your local working copy with:

git apply /path/to/some-changes.patch

And that’s it! The changes are now in your working copy and ready to be staged/commit/pushed :)

@jplindgren
Copy link
Author

jplindgren commented Aug 27, 2020

From GitHub for example look for the raw patch or diff.
To see a PR patch, just add ".patch" to the end of the pull request URL.

curl https://patch-diff.githubusercontent.com/raw/{user}/{repo}/pull/1274.patch?token={token} | git apply

@jplindgren
Copy link
Author

Cleaning things after
git reset --hard HEAD
git clean -f -d

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