"This is not a valid patch file." for a patch file that I *just* created.
Asked by cdbarlow:

No matter what I do, it says:

```
This is not a valid patch file.
```

I just created a patch file from either a working copy or a commit using Create Patch yet it says that. I used to use patch files all the time for several years this way but now I can never get the Apply Patch to work.


Answers:
author: Kubilay Erdogan
body: So it turns out that it's not a bug but a feature(!). When it's not working copy, Sourcetree appends more info to the patch file (then it's not a patch file anymore...) so when I remove those information, the patch works fine.

For instance, here is the patch file Sourcetree creates from an old commit:

```
From 24e5d114c5809c3cad52f1c2ef693fa07b84cb7e Mon Sep 17 00:00:00 2001
From: Kubilay Erdogan <>
Date: Thu, 21 Feb 2019 15:42:23 +0100
Subject: [PATCH] Add todo.

---
project/Class.swift | 1 +
1 files changed, 1 insertions(+)

diff --git a/project/Class.swift b/project/Class.swift
index 00806946..742f52ef 100644
--- a/project/Class.swift
+++ b/project/Class.swift
@@ -335,6 +335,7 @@ extension Class {
case foo

// New definitions
+ // TODO: Create documentation for these

case bar

-- 
2.18.0
```

And I get "This is not a valid patch file" error when I try to apply the patch.

But if I remove the header and footer info (Text before the first \`diff\` and the last two lines (double dashes and version info(?)) then it works.

So the working patch file is:

```
diff --git a/project/Class.swift b/project/Class.swift
index 00806946..742f52ef 100644
--- a/project/Class.swift
+++ b/project/Class.swift
@@ -335,6 +335,7 @@ extension Class {
case foo

// New definitions
+ // TODO: Create documentation for these

case bar

```

Hope this helps someone.

comments: author: Simon Salomons
body: You just saved me a full days work. Thank you!


author: Deniss Kaibagarovs
body: You are the best!


author: Emre Serbetcioglu
body: I'm having the same issue.

macOS Mojave(10.14.2)

SourceTree v 3.0.1 (205)

comments: author: Omid Zamani
body: same here.


author: Sven Burkert
body: Same here. Patch created by SourceTree can be applied in console with "git apply".


author: Deniss Kaibagarovs
body: ![31843u.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/54994i96A1D6D5BDFF8F59/image-size/large?v=v2&px=999 "31843u.jpg")


author: Charlie McGeorge
body: I had the same issue on MacOS 10.14.2 and SourceTree 3.0.1 (205).

I downloaded SourceTree 3.0.0 (300) and the same issue was present there as well.

I downloaded SourceTree 2.7.4 (175) and the patch functionality worked fine. Patches generated by 2.7.4 and even patches generated by 3.0.0 were applied correctly. This would imply that the message "This is not a valid patch file" is actually incorrect and the bug is in the patch file parser, not the patch file generator. Hope this helps.


author: Greg Harrington
body: Does anyone know how to get SourceTree's attention on this bug?


author: Charlie McGeorge
body: Fixed in 3.2!


author: gchaney
body: Ran into same problem today several times. SourceTree 3.1.2 on a Mac.

My patch includes diff for four files.

I tried the suggestion above "remove the header and footer info (Text before the first \`diff\` and the last two lines (double dashes and version info(?))" . but the file still does not work, i still get "this is not a valid patch file"


author: Pierre-Yves Bertholon
body: I've the issue in May 2019.

- Sourcetree Mac 3.1.2 (216).

- Use embedded Git 2.20.1.

- macOS Mojave (10.14.3)

The test to reproduce it:

- "Actions / Create Patch...". Save the patch file.

- "Actions / Apply Patch...". Select the previous created patch file.


author: kobizz
body: I'm having the same issue. Just wondering how long it will take to the SourceTree team to 'apply a patch' and resolve that annoying bug.

SourceTree 3.0.1 (205) on macOS Mojave (10.14.4)


author: bgannin
body: Hi Christopher,

What version of Sourcetree are you using? What platform are you on?

Brian Ganninger  
Senior Mac Developer, Sourcetree

comments: author: Greg_Harrington
body: I am having the same issue. Version:

MacOS

3.0.1 (205)


author: dancorp0711
body: Same problem here too. I had to roll back to 2.x to get patching to work.


author: jamesgosbell
body: I also get this issue. I create a patch file the usual way through sourcetree but I can't apply it.

Mac OS

3.0.1 (205)


author: DimitriC
body: I have the exact same issue.

Mac OS

3.0.1 (205)


author: IhorZabrotsky
body: The same thing for me.

MacOS

v3.0.1 (205)


author: Toon Van de Putte
body: I have the same issue. MacOS 10.13.3 and SourceTree 3.0.1 (205)


author: Kubilay Erdogan
body: Same issue. Mojave, ST 3.0.1 (205)

Quite surprised that this has been going on for a while. I will try to downgrade to a version that actually works. Such a shame.


author: Sagar Dagdu
body: I had the same issue on 3.1, downgraded to 2.7.4, and it is working fine. Seems to be a bug in the patch parser.


author: Guy
body: Same issue in 3.0.1...  

MacOS Mojave 10.14.3


author: Kevin Prichard
body: Broken here as well, SourceTree 3.1.0, on macOS 10.14 (18A391), Darwin 18.0.0 kernel.


author: Max
body: Same issue - SourceTree 3.1 (210)


author: 4np
body: Same here, SourceTree 3.1.1 (213) on macOS 10.14.4 Mojave using embedded Git 2.20.1. Switching over to System git (2.21.1) does not help.

If you remove the header from the patch file (e.g. the part up to the first diff statement) it works.