Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active April 25, 2026 14:06
Show Gist options
  • Select an option

  • Save nepsilon/22bc62a23f785716705c to your computer and use it in GitHub Desktop.

Select an option

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

@ahwelp

ahwelp commented Apr 5, 2020

Copy link
Copy Markdown

If you want to get the diff from a remote place without storing the file:

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

@johncmunson

Copy link
Copy Markdown

Does this work when your patch spans multiple files, new files, deleted files, directories, etc?

@shad

shad commented Apr 22, 2020

Copy link
Copy Markdown

Yes.

@wenijinew

wenijinew commented Oct 24, 2020

Copy link
Copy Markdown

Hi,

Did you ever face and solve the problem as follows?

git diff c5d633f7 relativepath/to/file > ~/p.patch
git apply --check ~/p.patch
error: patch failed: relativepath/to/file:1
error: relativepath/to/file: patch does not apply

@ievgen-pavlenko

ievgen-pavlenko commented Nov 16, 2020

Copy link
Copy Markdown

Try to use verbose mode git apply --verbose ~/p.patch
it will show more detailed info

To solve my problem I added --ignore-whitespace flag

@guettli

guettli commented May 27, 2021

Copy link
Copy Markdown

I get:

git apply ~/tmp/cherry-pick.patch
error: foo/client.py: No such file or directory
error: stand/foo/test_client.py: No such file or directory

But this works:

patch -p0 < ~/tmp/cherry-pick.patch 

@simanga-dev

Copy link
Copy Markdown

Hello. I have a patch diff in .diff format, git give me errors when I apply it and I want to manual solve the errors.
How can I view what the patch is trying to do and my local state of the project in diff mod so that I can fix the changes manual

@vijayhardaha

Copy link
Copy Markdown

@H3ndry you can open your patch file in any text editor and can see changes and file informations.

@coccoinomane

Copy link
Copy Markdown

Thanks for the snippets!
To make then work with changes to binary files, I had to run git diff --binary > some-changes.patch.

@iamshouvikmitra

Copy link
Copy Markdown

If you have a commit id, then you can also use

git show bc6626756 > ./my-changes.patch

and to restore use

git apply ./my-changes.patch

@rmmgc

rmmgc commented Feb 16, 2023

Copy link
Copy Markdown

git diff will always add file path to the patch like this.

diff --git a/<file_path> b/<file_path>
...

If you want to reuse the same patch on the file that does not match this file_path, you can do it with patch command.

patch <file_to_patch> <file_with_patch>

@MayuriVidekar

Copy link
Copy Markdown

I created patch file for stash using following command:
git stash show "stash@{0}" -p > '..\stashes\changes.patch'

When I try to apply the same using
git apply '..\stashes\changes.patch'

Got below error:
error: No valid patches in input (allow with "--allow-empty")

Path to the patch file is correct. Can someone please help?

@ssi-anik

ssi-anik commented May 23, 2023

Copy link
Copy Markdown

If you run git diff it will show changes that were tracked previously. But newly added files are not shown in the diff. To create a patch, I did the following.

  • git add -A # Add everything to the staging area.
  • git diff --staged --patch > changes.patch # --staged shows all the changes including new files
  • git reset # Reset to the old state

Now, I can apply the changes.


Edit: I just checked git diff --staged --patch and git diff --staged by creating two new patch files, and comparing them with the diff command. I don't find any difference.

@BobToninho

Copy link
Copy Markdown

@ssi-anik Didn't think about untracked files—I always forget about them when thinking about git command, thanks!

I believe you can simplify your solution even more by using intent-to-add like this:

git add --intent-to-add --all
# or
git add -NA

@mrpointzby

mrpointzby commented May 10, 2024

Copy link
Copy Markdown

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
Copy Markdown

Hi

@rajavelraj

Copy link
Copy Markdown

Git format-patch- 1- head

@JulienCochuyt

Copy link
Copy Markdown

@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
Copy Markdown

@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