Skip to content

Instantly share code, notes, and snippets.

@scodx
Last active November 11, 2019 16:38
Show Gist options
  • Save scodx/183fb21e9bf51c64b6e3690b8bfd4c68 to your computer and use it in GitHub Desktop.
Save scodx/183fb21e9bf51c64b6e3690b8bfd4c68 to your computer and use it in GitHub Desktop.

Git Patches

Generate a patch from un-commited files:

git diff > file.patch

But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (but don't do the commit), and then:

git diff --cached > file.patch

Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

git diff --cached --binary > file.patch

Generate a patch between two commits

git diff commitid1 commitid2 > file.patch

Generate a patch between the HEAD and a specific commits

git diff commitid1 > file.patch

Generating a patch from excluded files

diff -u path/to/original_file path/to/new/modified_file > file.patch

Generate a patch from git stash

git stash show -p > file.patch

Apply the patch:

git apply mypatch.patch

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