Patching with Git

Patching with Git

There are times when we wanted to update our web application on the server-side but our shared hosting server blocked our outgoing SSH ports preventing us from using git to update the web applications on a shared hosting. So instead of using git with deploy key on the server, we go for server-side update with git diff to create a patch and git apply to apply the patch. In order for this to work, SSH access to the server is required.

Creating a Patch File

Option 1: Creating patch from working copy (not committed to Git yet): git diff > patch.diff

Option 2: Creating patch starting from one revision until another newer revision, remember to replace <from-commit-hash> and <to-commit-hash> to the revision hash you need. To get a list of revision hashes, use git log: git diff <from-commit-hash> <to-commit-hash> > patch.diff

Applying a Patch

  1. Copy the patch file over to the server either through SCP or FTP
  2. cd over to the root directory of application where you are applying the patch

Once you are logged into the remote server, you can patch the original file specified in the original git diff command or you can patch a different file from the command.

Patching the original file

git apply --ignore-space-change --ignore-whitespace patch.diff, where patch.diff is the patch file

Patching a different file

patch -p1 <file-to-patch> patch.diff, where:

  • patch.diff is the patch file
  • <file-to-patch> is the file to apply the patch
Consent Preferences