Git: Generating patches

Generate patch for given commit

1. git format-patch

git format-patch -1 <commit_id>
The patch file has a 4 digit numeral followed by the commit message separated by hyphens. 

Example:
For the following commit:
commit 6d432152b9e5627532c52d6f1c9959cb3be52e29
Author: Bruce Momjian <bruce@momjian.us>
Date:   Mon Jul 1 12:46:13 2013 -0400
    Update LSB URL in pg_ctl
    Update Linux Standard Base Core Specification 3.1 URL mention in pg_ctl
    comments.

git format-patch -1 generates patch file 0001-Update-LSB-URL-in-pg_ctl.patch
 

2. git show

 git show <commit_id>
This will will simply display the patch in stdout. We can redirect this output to a patch file. In the following command, testfile.patch will have the patch for the specified commit.
git show 6d432152b9 > testfile.patch

Special Keyword HEAD

1. git format-patch

HEAD refers to the latest commit. The following will generate 2 patch for the last two commits.
git format-patch -2 HEAD
~n is appended to HEAD to refer to a nth commit before latest one. The following command generates a patch for the 2nd commit before the latest.
git format-patch -1 HEAD~2

2. git show

The following will display the patch for latest HEAD 
git show HEAD
The following will display the changes since the last n commits 
git show HEAD~n

n commits before specified

git format-patch

git format-patch -n <commit_id>
This command will generate n patch files for each of the n commits starting from (n-1) commit before the specified <commit_id>
For n > 2, patches will be generated from (n-1) commits before the specified commit.

git format-patch -3 6d432152b9 generates 3 files:
       0001-Optimize-pglz-compressor-for-small-inputs.patch
       0002-Remove-undocumented-h-help-option.patch
       0003-Update-LSB-URL-in-pg_ctl.patch

0001 file is patch for 2 commits before 6d432152b9 and 0003 file is the patch for specified commit.

We can avoid specifying commit id when we want patches for the last n commits. The following generates patch for the last 7 commits.

git format-patch -7
We can avoid specifying commit id when we want patches for the last n commits. The following generates patch for the last 7 commits.


Difference Between 2 commits

git diff

To check the changes between 2 known commits use git diff command, this output again displayed on stdout and so will need to be redirected to a file 
git diff <from_commit> <to_commit> 
Note that the changes of the <from_commit> are not included. The <from_commit> should be older (committed before) than <to_commit>. If we reverse the <from_commit> and <to_commit>, we would be getting a patch to reverse all the changes made.
git diff 031cc55b 6d432152 > diff.patch

No comments:

Post a Comment