Featured image of post How to use diff tools to patch files?

How to use diff tools to patch files?

Introduce the `diff` command and `git diff` command to patch files

diff and patch command to patch files

  1. use diff command to generate patch file
1
diff -u old_file new_file > change.patch
  1. apply patch to change old file to new one
1
patch old_file change.patch

-u parameter will make the diff output use unified output format, which is easy-to-read like git diff’s output. It is optional.

git diff usage

  1. get the changes between the workspace and staging area
1
git diff > change.patch
  1. get the change between an old version of your local repository and new one.
1
git diff old_commit_hash new_commit_hash > change.patch
  1. diff for one file but not whole folder
1
git diff old_commit new_commit $file_name > change.patch
  1. apply patches
1
git apply change.patch

git reflog will show a more short commit hash code which is enough for the git diff command