Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active December 3, 2024 18:26
Show Gist options
  • Save nepsilon/22bc62a23f785716705c to your computer and use it in GitHub Desktop.
Save nepsilon/22bc62a23f785716705c 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

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 :)

@mrpointzby
Copy link

mrpointzby commented May 10, 2024

I can export the code change to .patch file, but the change only include the main part, Now I change the submodule and want to export the code change in submodule to the same .patch file so that I can apply the patch file for all code changes on new project including the submodule part, are there some commands to do this?

@rajavelraj
Copy link

Hi

@rajavelraj
Copy link

Git format-patch- 1- head

@JulienCochuyt
Copy link

@MayuriVidekar, same error here on Windows. I'm pretty sure it used to work. Resorting now to git checkout -p the stash rev.

@KoltesDigital
Copy link

@MayuriVidekar @JulienCochuyt surely you're using PowerShell < 7.4. Upgrade or see https://stackoverflow.com/a/70283963/14098985 for workarounds.

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