DEV Community

Cover image for diff and patch explained
Akram Narejo
Akram Narejo

Posted on โ€ข Edited on

4 2

diff and patch explained

diff and patch are the useful commands which help in updating the file with current changes.

  • diff tells the difference between two files
  • Patch is the difference of two files conceptually but as for command patch updates the file with given changes

Let's create a file demo.py with a command nano demo.py with a print statement inside.

print(hello world)

If we execute demo.py python3 demo.py we will get an error

File "demo.py", line 2
print(hello world)
SyntaxError: invalid syntax

So in this case Let's make an other file demo_fixed.py and correct the error.

print('hello world')

Now if we execute demo_fixed.py file we get the output

hello world

Let's compare the both of files demo.py and demo_fixed.py to see the difference.

diff -u demo.py demo_fixed.py

we get the following changes

--- demo.py 2020-07-30 12:35:50.200813874 +0500
+++ demo_fixed.py 2020-07-30 12:35:35.144740070 +0500
@@ -1,2 +1,2 @@
-print(hello world)
+print('hello world')

- means statement is deleted & + means the statement is added.
Let's write the changes to another file so we can update the changes with original file.

diff -u demo.py dem_fiexed.py > changes.diff

> writes the data & < reads the data

Now let's apply the patch or changes to our actual file

patch demo.py < changes.diff

If we execute the demo.py file now

Python3 demo.py

we get

hello world

Finally we updated our demo.py file with the given patch of changes.

This is my first post so please let me know about the content and if you find it helpful please do appreciate.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong ยท web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video ๐ŸŒถ๏ธ๐Ÿ”ฅ

Top comments (2)

Collapse
 
jakeerc profile image
Jakeer โ€ข

Salamalaikum, Jazakallah khair

Helpfull

Collapse
 
akramnarejo profile image
Akram Narejo โ€ข

wa alaykums-salam brother thanks.

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video ๐Ÿ“น๏ธ

๐Ÿ‘‹ Kindness is contagious

If this article connected with you, consider tapping โค๏ธ or leaving a brief comment to share your thoughts!

Okay