Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to be able to use diff & patch in order to write new lines to a file. To clarify what I mean a bit:

Original file: test1.repo




New file: test2.repo




So I run the command diff -u test1.repo test2.repo > repo.patch to find the differences, simple enough. Looking at the repo.patch file I just made, I see the output:





If I use the patch as is though, it will write over with what is effectively just the data I have in test2.repo. Is there a way to get patch to only write new lines (anything starting with a +)? I'd like to add lines to the original repository, but not destroy any customization they've already done to their file.

share|improve this question
What about replacing these remove lines in the patch by running e.g. the sed script sed 's/^-\([^-]\{2\}\)/ \1/' on it? – eldering Apr 16 '13 at 16:26
Might work for this example but would fail for lines that actually start with -- for some reason. Personally I just manually edit the patch to sort out conflicts. – frostschutz Apr 16 '13 at 16:59
You can use cat <(head -n 1 repo.patch) <(grep -ve '^-' repo.patch) | patch to address the problem pointed at by @frostschutz. But with these modified patch files I get the error patch: **** malformed patch at line 9: (thats the EOF), because you also need to modify the @@ -1,4 +1,6 @@ lines?! – mpy Apr 16 '13 at 17:25
You're right @mpy, by directly modifying the patch file like this I would need to update the location reference line too. It's easy to do by hand, but I need the whole process to be automated, hence the original question. – Mono Apr 16 '13 at 17:38
For the given input, sort -u test1.repo test2.repo could also work. – frostschutz Apr 16 '13 at 20:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.