vrish88
vrish88

Reputation: 21477

How to create a git patch from the uncommitted changes in the current working directory without creating a commit?

Say I have uncommitted changes in my working directory. How can I make a patch from those without having to create a commit?

Upvotes: 1413

Views: 953827

Answers (9)

Mic Ho
Mic Ho

Reputation: 121

all the above works.

BUT,

On Windows, please DO NOT try to do
git diff > name.patch or git show > name.patch
under PowerShell!
Because the PowerShell redirect will default convert your output as UTF-16 encoding, and a git apply does not regard it as a valid patch!

BTW, if you want to generate a git patch with binary changes, please do like these:

git show --binary COMMIT_HASH
# or
git diff --binary HASH_A..HASH_B

summary:

Using git with PowerShell maybe a disaster!

Upvotes: 2

Eugen Konkov
Eugen Konkov

Reputation: 25223

I like:

git format-patch HEAD~<N>

where <N> is number of last commits to save as patches.

The details how to use the command are in the DOC

UPD Explanation of fix when GIT_PREFIX is called from top level Git directory

UPD
Here you can find how to apply them then.

UPD For those who did not get the idea of format-patch
Add alias:

git config --global alias.make-patch '!bash -c "cd ${GIT_PREFIX:-.};git add .;git commit -m ''uncommited''; git format-patch HEAD~1; git reset HEAD~1"'

Then at any directory of your project repository run:

git make-patch

This command will create 0001-uncommited.patch at your current directory. Patch will contain all the changes and untracked files that are visible to next command:

git status .

Upvotes: 36

Samet &#214;ZTOPRAK
Samet &#214;ZTOPRAK

Reputation: 3374

uncomminetted

git diff --cached > name.patch

committed (much more useful)

git diff HEAD~commit_count > name.patch

Upvotes: 2

jcarballo
jcarballo

Reputation: 29153

If you haven't yet commited the changes, then:

git diff > mypatch.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 (git add each file, or just git add .) but don't do the commit, and then:

git diff --cached > mypatch.patch

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

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

You can later apply the patch:

git apply mypatch.patch

Upvotes: 2726

sigjuice
sigjuice

Reputation: 29787

git diff for unstaged changes.

git diff --cached for staged changes.

git diff HEAD for both staged and unstaged changes.

Upvotes: 626

Anshu Kumar
Anshu Kumar

Reputation: 646

We could also specify the files, to include just the files with relative changes, particularly when they span multiple directories e.x.

git diff ~/path1/file1.ext ~/path2/file2.ext...fileN.ext > ~/whatever_path/whatever_name.patch

I found this to be not specified in the answers or comments, which are all relevant and correct, so chose to add it. Explicit is better than implicit!

Upvotes: 7

Ionel Sirbu
Ionel Sirbu

Reputation: 587

To create a patch with both modified & new files (staged) you can run:

git diff HEAD > file_name.patch

Upvotes: 57

gitster
gitster

Reputation: 137

If you want to do binary, give a --binary option when you run git diff.

Upvotes: 10

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

git diff and git apply will work for text files, but won't work for binary files.

You can easily create a full binary patch, but you will have to create a temporary commit. Once you've made your temporary commit(s), you can create the patch with:

git format-patch <options...>

After you've made the patch, run this command:

git reset --mixed <SHA of commit *before* your working-changes commit(s)>

This will roll back your temporary commit(s). The final result leaves your working copy (intentionally) dirty with the same changes you originally had.

On the receiving side, you can use the same trick to apply the changes to the working copy, without having the commit history. Simply apply the patch(es), and git reset --mixed <SHA of commit *before* the patches>.

Note that you might have to be well-synced for this whole option to work. I've seen some errors when applying patches when the person making them hadn't pulled down as many changes as I had. There are probably ways to get it to work, but I haven't looked far into it.


Here's how to create the same patches in Tortoise Git (not that I recommend using that tool):

  1. Commit your working changes
  2. Right click the branch root directory and click Tortoise Git -> Create Patch Serial
    1. Choose whichever range makes sense (Since: FETCH_HEAD will work if you're well-synced)
    2. Create the patch(es)
  3. Right click the branch root directory and click Tortise Git -> Show Log
  4. Right click the commit before your temporary commit(s), and click reset "<branch>" to this...
  5. Select the Mixed option

And how to apply them:

  1. Right click the branch root directory and click Tortoise Git -> Apply Patch Serial
  2. Select the correct patch(es) and apply them
  3. Right click the branch root directory and click Tortise Git -> Show Log
  4. Right click the commit before the patch's commit(s), and click reset "<branch>" to this...
  5. Select the Mixed option

Upvotes: 111

Related Questions