Applying patches with Git and curl or wget

There are some methods to apply the code from a pull request (PR) to a base branch.

E.g., if you want to test this PR, you can use the GitHub CLI and this command in your local repository.

gh pr checkout 3

But this only works at GitHub, so you can do something similar, and valid for different places, using the patch from the PR.

Getting the patch

If you add the “.patch” text to the PR URL, you are going to get the patch file from this PR:

You can do something similar with the content from an individual commit, not from all commits of a PR:

Applying the patch

To apply one of this patches to the current branch, you can get the patch with curl or with wget and then apply the patch:

curl -L -O https://github.com/amieiro/GlotPress/pull/3.patch
wget https://github.com/amieiro/GlotPress/pull/3.patch

And then you can apply the patch

git apply 3.patch

Finally, you need to remove the patch file:

rm 3.patch

Applying the patch in one step

You can do the whole process in one step, using Unix pipes with curl:

curl -L https://github.com/amieiro/GlotPress/pull/3.patch | git apply -v

Or with wget:

wget -q -O - https://github.com/amieiro/GlotPress/pull/3.patch | git apply -v

In both situations, you don’t need to remove any local file.

Another relevant servers

You can do the same with another relevant players, like GitLab, Bitbucket,…

GitLab

To obtain the patch, you need to add the .patch text to the URL.

Bitbucket

To obtain the Bitbucket patch, you need to use a URL like this: https://bitbucket.org/api/2.0/repositories/GROUP/PROJECT/pullrequests/ID/diff (more info here).

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.