1

A programmer friend sent me a txt-file, containing diff information. Like this, from the beginning of the txt-file content:

diff --git a/myfolder/MyClass.java b/myfolder/MyClass.java
index 1234aa0d0554..5678bcasas 112233
--- a/myfolder/MyClass.java
+++ b/myfodler/MyClass.java
@@ -58,7 +58,7 @@ public class MyClass{
        [somecode here]
 - [some row] 
 + [some row]       

etc.

How can I apply these changes to git by using command line or source tree? I'm quite new to git and I know you can apply .patch files. Can I convert this content to a patch file or what is the best way to do this? It is just a Java home project.

Steve Waters
  • 3,348
  • 9
  • 54
  • 94
  • Does this answer your question? [How to apply a patch generated with git format-patch?](https://stackoverflow.com/questions/2249852/how-to-apply-a-patch-generated-with-git-format-patch) – Joe Jun 26 '20 at 07:40

1 Answers1

4

To apply a patch just use git apply

git apply /path/to/some-changes.patch

If you have only the diff, you can add some missing information

From: John Doe <email>
Date: Wed, 6 May 2020 22:53:29 +0200
Subject: the commit title

commit detail

diff --git a/myfolder/MyClass.java b/myfolder/MyClass.java
index 1234aa0d0554..5678bcasas 112233
[...]
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • how can I make a patch file of the described txt-file contents? – Steve Waters Jun 26 '20 at 08:57
  • 1
    @SteveWaters I have added an example on how to add info, you can use `git format-patch` to see some patch examples – Ôrel Jun 26 '20 at 09:18
  • 1
    @SteveWaters : `git apply` looks at the content of the file you give it ; it could be named `some-changes.patch`, `some-changes.txt`, or even `some-changes.jpg`, as long as its content looks like a diff (e.g : a text file, which first mention file names, then ranges of rows, then rows prefixed with "+" or "-" ...), `git apply some-changes.jpg` will work. – LeGEC Jun 26 '20 at 09:22
  • in the end I could do it with `git apply --ignore-space-change --ignore-whitespace mychanges.txt` – Steve Waters Jun 29 '20 at 06:19