Creating Your Own Patch
Now let's learn how to create our own patch. We'll make changes to a file and generate a diff file that can be used to apply those changes to other copies of the file.
First, create a new text file:
cd ~/project
cat > new_file.txt << 'EOF'
This is line one.
This is line two.
This is line three.
EOF
Now, create a copy of this file that we'll modify:
cp new_file.txt new_file_modified.txt
Edit the modified file to make some changes:
nano new_file_modified.txt
Change the second line to "This is the MODIFIED line two." and save the file by pressing Ctrl+O, Enter, then Ctrl+X.
Now, create a patch file that represents the differences between these two files:
diff -u new_file.txt new_file_modified.txt > patches/my_patch.diff
Let's examine the patch we've created:
cat patches/my_patch.diff
You should see a diff output showing the changes you made to the file.
Now, let's revert the modified file back to its original state and then apply our patch to test it:
cp new_file.txt new_file_modified.txt
patch new_file_modified.txt < patches/my_patch.diff
Verify that the patch was applied:
cat new_file_modified.txt
You should see that the second line was changed to "This is the MODIFIED line two."