Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ganapathichidambaram/3504e31fc8ba809762b3a0d9d9ef8ec2 to your computer and use it in GitHub Desktop.
Save ganapathichidambaram/3504e31fc8ba809762b3a0d9d9ef8ec2 to your computer and use it in GitHub Desktop.
To Generate formatted patch file and apply using git command

Generate Patch

Generate Last one Patch based on Commit ID:

To generate the formatted patch of last one commit.

git format-patch master -1 aaa77c88f5e8f1c6c6dde963e77072d4f55d6a3e -o patches
  • format-patch - formatted patch file generation. Commit Author,Commit Date and message etc.

  • -1 - Last one Commit

  • aaa77c88f5e8f1c6c6dde963e77072d4f55d6a3e - Commit ID which should be key-point to generate after/before that.

  • -o patches - Generate the patch to specified Output folder.

Generate Last few Patch

Generate last four commit to specified output folder based on each commit.
Each commit should be separate patch file with commit message-named.

git format-patch -4 -o patches

Note: By Default it would ignore if there is any empty commit like only whitespace modification.

Generate Last few Patch on Single Patch File

To Generate last four commit to standard output window and redirect into single patch file to avoid generation of mulitple patch file.

git format-patch -4 --stdout > patches/0005_phnbk_json_load_pycurl_commit.patch

Generate the patch using Linux Diff command.

To generate the patc file from non-git folder by using diff command which is available in linux distribution.

diff -Naur old-folder new-modified-folder

It would generate patch file for all the changes between entire folder in any sub-directory.

Also if we want to exclude any folder or file then by using exclude option we can exclude the differnece to be generated on patch file.

diff -Naur old-folder new-modified-folder --exclude=db --exclude=.env

Make sure old-folder should be specified first and after new modified folder as second.

To Apply the Patch file

To Apply formatted patch file need to use git am to apply the patches. Also make sure there won't be any whitespace. Otherwise apply patch would fail. To Avoid those whitespace warning add whitespace=nowarn option on git am command.

Without Whitespace

git am patches/0001-crm-utility-for-phonebook-upload.patch

Avoid Whitespace Warning

git am --whitespace=nowarn patches/0001-crm-utility-for-phonebook-upload.patch

After successfully apply the patch verify the patch by using mentioned below command.

Verify with commit message and details with patch file.

git log

Rollback Commit

It would clear all the modification to particular commit ID including git log commit messages without recording those commit or revert.

git reset --hard d9c5befd344bcb466718da161fa9a8dd0ce4af06

But If we want to record the reverting those commit then git revert would revert and update the commit with reverting commit messages.

git revert d9c5befd344bcb466718da161fa9a8dd0ce4af06

Back to Original Head

It would rollback all the changes like commit and revert to original HEAD with recent commit message.

git reset --hard ORIG_HEAD

Apply patch with unix patch utility instead of git

With git formatted patch file we can apply the changes on code even without git by using linux patch command.

Apply patch using mentioned below command.

patch -pNUM < filename-to-patch.patch

Where NUM means that how many root directories you are going to remove in patch file. For example, in patch file there is file patch like a/foldername/service.php. If we use -p1 option, then patch will be applied file path foldername/service.php. If we use -p2 option, then patch will be applied to file path service.php. Also make sure your current working directory should have file according to number specified. For git format-patch -p1 is necessary and command need to execute from the source code main folder.

  • Patch without stdout
patch -p0 -i filename-to-patch.patch
  • Rollback the applied patch
patch -R -p0 -i filename-of-applied-patch.patch
@kunalvaidya
Copy link

Thanks. the "Generate Last few Patch" was very useful to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment