0

I'm trying to learn git patching, so I set up a test repo and made a few commits.

I then created a patch: git format-patch -1 HEAD --stdout > changes.patch

Next, I checked out a new branch and tried to use changes.patch: git am .\changes.patch. It gives me the error Patch format detection failed.

I searched here, and found this related question.

So I tried git apply .\changes.patch. It gives me error: unrecognized input.

The patch file seems fine to my untrained eye:

From 1c054c05bc528afbd929a1744fcacf9d70069246 Mon Sep 17 00:00:00 2001
From: MyUsername <my.email@gmail.com>
Date: Sat, 4 May 2019 22:43:32 -0400
Subject: [PATCH] Commit 4

---
test.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/test.txt b/test.txt
index 58ef11d..763fe4e 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 Initial commit
 Commit 2
-Commit 3
\ No newline at end of file
+Commit 3
+Commit 4
\ No newline at end of file
-- 
2.21.0.windows.1

I thought it might be that the patch is in the same directory as the repo, so I moved it to another directory. The results are the same.

I also noticed that many people had a < in the command, so I tried it: git am < ..\changes.patch. Apparently that's not valid syntax.

This is using 64 bit git on Windows with PowerShell.

Any ideas?

Danation
  • 743
  • 8
  • 20

2 Answers2

3

Try cat .\changes.patch | git am.

But I have no idea why git am .\changes.patch can't work.

Update:

changes.patch is in USC-2 Little Endian by default. After changing it to UTF-8, git am .\changes.patch works.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Interesting. It did work! I'll try to see if I can find out why that is. – Danation May 05 '19 at 07:41
  • 1
    @Danation after some research, I find it's caused by the encoding of `changes.patch`. Powershell generates the patch in `USC-2 Little Endian`. After changing it to `UTF-8`, `git am .\changes.patch` works. – ElpieKay May 05 '19 at 07:48
  • 1
    Nice to have the mystery solved! I thought it was strange that I'd have this encoding issue, so I searched around more. According to this, https://stackoverflow.com/a/13751617/1192877, the culprit is `--stdout` and how PowerShell handles the output. I tried it out, and it's true: without `--stdout` the resulting file works properly. – Danation May 05 '19 at 07:58
  • Is it UCS-2, or UTF-16-LE? (The difference is that UCS-2 does not allow surrogate escapes.) – torek May 05 '19 at 17:36
  • @torek Notepad++ tells that it’s USC-2 Little Endian. – ElpieKay May 06 '19 at 00:45
  • @torek And `file changes.patch` prints `Little-endian UTF-16 Unicode text, with CRLF, CR line terminators`. – ElpieKay May 06 '19 at 02:16
  • So, the way to tell which it really is would be to ask PowerShell to write a file with Unicode characters that require surrogate escapes to be used, and see whether they are. – torek May 06 '19 at 05:13
0

Do you want to create a patch from HEAD? Then you can simply do as follows:

To save the patch:

git show HEAD > /some/location/patch.txt

To apply the patch:

git apply /some/location/patch.txt

If you want to create a patch from a bunch of commits you can do:

git diff OLDEST_COMMIT..NEWEST_COMMIT > /some/location/patch.txt

This way you can, for instance, create a patch containing the changes from a feature branch:

git diff origin/master...origin/new_feature > /some/location/feature.patch

Notice the three dots, they mean you want the changes contained in origin/new_feature with respect to the common ancestor of the two branches.

Manuel Schmidt
  • 2,178
  • 2
  • 15
  • 19
  • My goal is to learn how to use `git format-patch` and `git am` for a particular project that requires collaboration through email. In this case I was just using HEAD as a test case. – Danation May 05 '19 at 07:38
  • If you want to send the patches as attachments, you can do it this way. You can share this files in any way you want. – Manuel Schmidt May 05 '19 at 07:42
  • The thing I like about `git am` is that it preserves the commit history of the contributor. As I understand it, `git apply` acts as if the receiver is the author. – Danation May 05 '19 at 07:45
  • 1
    Ok. I wasn't familiar with git format-patch / git am. I occasionally use `git apply` and don't need those additional information. Good to know. – Manuel Schmidt May 05 '19 at 07:56