Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

diff

patch

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 
like image 533
Rafid Avatar asked Jan 07 '11 09:01

Rafid


People also ask

What is git format patch?

Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository.

What is git diff patch?

Git diff is a command used to output the changes between two sources inside the git repository. The data sources can be two different branches, commits, files, etc.

What is a git diff?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

What is the difference between the git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.


2 Answers

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.

like image 160
Sylvain Defresne Avatar answered Oct 15 '22 04:10

Sylvain Defresne


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.

like image 26
Abizern Avatar answered Oct 15 '22 04:10

Abizern