Search code examples
gitdiffgit-diffgit-patch

Applying git diff contents from a txt file


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.


Solution

  • 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
    [...]