What is the difference between 'git format-patch and 'git diff'?

46,445

Solution 1

A patch created with git format-patch will also include some meta-information about the commit (committer, date, commit message, ...) and will contains diff of binary data. Everything will be formatted as a mail, so that it can be easily sent. The person that receive it can then recreate the corresponding commit with git am and all meta-data will be intact. It can also be applied with git apply as it is a super-set of a simple diff.

A patch crated with git diff will be a simple diff with context (think diff -u). It can also be applied with git apply but the meta-data will not be recreated (as they are not present).

In summary, git format-patch is useful to transmit a commit, while git diff is useful to get a diff between two trees.

Solution 2

From the manuals git-format-patch prepares patches suitable for email submission, while git-diff shows changes.

They are two different things and have different purposes, they just happen to output a patch format. But git-format-patch adds data about a commit (date, author, commit message) and bundles it up into a format that is suitable for sending as a Unix mail message (although these are just files, so they can be sent to other methods and still applied by git-am).

Also git-format-patch generates a patch file for each commit in the range that you specify. These changes will be added as commits to your repository with git-am.

git-diff just shows the diff between the two states you ask for, and can be used to create a patch file. But this is just a normal patch file and applying the patch will just change the state of the working directory.

And yes, you can create a patch for your index that way.

Solution 3

The patch file can be generated with the git diff command, but comparing with the patch generated by the git format-patch command, the major differences are:

  1. No metadata about a commit (such as date, author, commit message, etc.) ;
  2. No statistics about the diff (diffstat, such as x files changed, y insertions(+), z deletions(-));
  3. No binary diffs, only textual diffs.

enter image description here

To generate the patch file for all changed files (in the index or the working directory):

git diff HEAD --binary > my.patch
# git diff + git diff --cached/staged == git diff HEAD

To apply the generated patch file:

# restore the changed files firstly
git restore --staged .
git restore .

# apply the patch to redo the changes 
git apply my.patch
# or
patch -p1 < my.patch
Share:
46,445

Related videos on Youtube

Rafid
Author by

Rafid

Software developer with passion. Started learning programming at around 10 years of age. Currently working @ AWS in Vancouver, British Columbia. Aside from my main job, I am trying to spend most of my free days developing my knowledge with Machine Learning. When I have time to spare, I enjoy reading mathematics and physics. When I am bored, I go for a walk or ride my bicycle (when Vancouver weather permits!)

Updated on February 21, 2020

Comments

  • Rafid
    Rafid about 4 years

    I don't see a difference between the output of 'git format-patch' and 'git diff', is there any? And won't I be able to use 'git diff' to produce a patch and then apply it using git apply?

    My problem is that I have changes added to the index, but apparently git format-patch only accepts commits, so if I can use the output of diff, then I can use this command to produce a patch for the changes in the index:

    git diff --cached > index.patch
    
  • Rafid
    Rafid over 13 years
    Thanks. What is special about UNIX mail so that they designed the patch based on it?
  • Sylvain Defresne
    Sylvain Defresne over 13 years
    There is nothing really special. This is just that git was designed by Linus Torvalds whose workflow involved sending and receiving patches by email for validation before integration into the Linux kernel.
  • Abizern
    Abizern over 13 years
    Git was designed by Linus Torvalds for keeping the Linux kernel in. Unix mail was a common format.