Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3318266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:41:07+00:00 2026-05-17T22:41:07+00:00

I’ve noticed this strange behavior of diff and patch when I’ve used them to

  • 0

I’ve noticed this strange behavior of diff and patch when I’ve used them to force one code base to be identical to another. Let’s say I want to update update_me to look identical to leave_unchanged. I go to update_me. I run a diff from leave_unchanged to update_me. Then I patch the diff into update_me. If there are new files in leave_unchanged, patch asks me if my patch was reversed! If I answer yes, it deletes the new files in leave_unchanged. Then, if I simply re-run the patch, it correctly patches update_me.

Why does patch try to modify both leave_unchanged and update_me?

What’s the proper way to do this? I found a hacky way which is to replace all +++ lines with nonsense paths so patch can’t find leave_unchanged. Then it works fine. It’s such an ugly solution though.

$ mkdir copyfrom
$ mkdir copyto
$ echo "Hello world" > copyfrom/myFile.txt
$ cd copyto
$ diff -Naur . ../copyfrom > my.diff
$ less my.diff

diff -Naur ./myFile.txt ../copyfrom/myFile.txt
--- ./myFile.txt        1969-12-31 19:00:00.000000000 -0500
+++ ../copyfrom/myFile.txt      2010-03-15 17:21:22.000000000 -0400
@@ -0,0 +1 @@
+Hello world

$ patch -p0 < my.diff

The next patch would create the file ../copyfrom/myFile.txt,
which already exists!  Assume -R? [n] yes
patching file ../copyfrom/myFile.txt

$ patch -p0 < my.diff
patching file ./myFile.txt

Edit

I noticed that Mercurial avoids this problem by pre-pending “a” and “b” directories.

$ hg diff
--- a/crowdsourcing/models.py   Mon Jun 14 17:18:46 2010 -0400
+++ b/crowdsourcing/models.py   Thu Jun 17 11:08:42 2010 -0400
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:41:07+00:00Added an answer on May 17, 2026 at 10:41 pm

    I believe the answer here is to execute your diff at the parent directory. Then use patch -p1 to strip this first segment. I believe this is why the strip option of patch actually defaults to 1 rather than 0. E.g. to use your example from above

    $ mkdir copyfrom
    $ mkdir copyto
    $ echo "Hello world" > copyfrom/myFile.txt
    $ diff -Naur copyto copyfrom > my.diff
    $ less my.diff
    
    diff -Naur copyto/myFile.txt copyfrom/myFile.txt
    --- copyto/myFile.txt    1970-01-01 12:00:00.000000000 +1200
    +++ copyfrom/myFile.txt    2010-10-19 10:03:43.000000000 +1300
    @@ -0,0 +1 @@
    +Hello world
    
    $ cd copyto
    $ patch -p1 < ../my.diff
    

    The only difference from your example is that I’ve executed the diff from the parent directory so that the directories being compared are at the same level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I am trying to understand how to use SyndicationItem to display feed which is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.