Patch
Yes.., i forgot it again. I just don;t patch enough to make it stick. So here is a short guide again.
diff
To patch you first have to make a diff, for a single file it is easy. Here are two example files :
old:
new:
A simple diff old new gives :
| human readable diff | |
|---|---|
But that's way too human readable, so make a unified diff : diff -u old new
| unified diff | |
|---|---|
Of course redirection would be your tool :
| and save that in a patch file | |
|---|---|
This last format can be used for patch, see next chapter:
apply patch
To apply a patch, you normally have the file old , and the patch and want : new.
No filenames needed, because they are in the patch file themselves, this command will turn old into new :| compare both | |
|---|---|
returns nothing. If you need to use a file that is named differently :
As you might have guessed 'new' is not used in any of these commands.
directories
On the diff side, this is easy, use the -r (recursive) option. But you need to diff something, so you need to have the original directory besides the altered one. A simple complete example:
mkdir old
echo "Hallo" >> old/file
cp -r old new
echo "extra regel" >> new/file
diff -ruN > dir.patch
The patch file will contain :
| patch content | |
|---|---|
Patching is done with :
| patch apply patch | |
|---|---|
The -pX (--strip=X) is mandatory, and means strip X parts from the path prefix. So the -p0 would do nothing but :
git
Making patches with git is rather easy if you use the git commands both ways :
| create git patch | |
|---|---|
This produces unified format diff, but with some extra baggage, like a/ and b/ before the filenames. You could 'patch' that away again with -p1, or use
| skip prefix | |
|---|---|
If you need to provide a 'patch' input. But the easiest is to use git again to 'apply' the patch ;
| apply a patch | |
|---|---|