11

How can I apply the patch from github?

I tried to compile minisat, but I came across two issues from the compilation with clang.

The first issue is solved in this github commit, it's forked from the original github. As the change is minute, I could easily patched the code to work manually.

The second issue is solved in this github (https://github.com/niklasso/minisat/pull/17), but the patch is not applied to the original source. I could manually update the code by copying the modified files, but it would be better if I can pull this patch into my local directory. Is it possible to do that with github? If so, how to do it?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
prosseek
  • 182,215
  • 215
  • 566
  • 871

3 Answers3

22

github provides patches for individual commits and pull requests (though I can't find the documentation for this).

You can generate the patch url by simply appending .patch to the end of the original url.

So, use https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch for the first, and https://github.com/niklasso/minisat/pull/17.patch for the second.

The general url github.com/original/url/id would become github.com/original/url/id.patch for generating the patch.

In terms of commands to run, this becomes

  1. Download the patches to your git repo

    wget --output-document=issue1.patch https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch
    wget --output-document=issue2.patch https://github.com/niklasso/minisat/pull/17.patch
    
  2. Apply the patches

    git apply issue1.patch
    

    Check the changes, add and commit. Repeat the same for patch 2.

You can check this blog post for a nice tutorial around creating and applying patches.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 3
    @prosseek, go to `https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch` and save the contents as `1.patch`, then go to `https://github.com/niklasso/minisat/pull/17.patch` and save the contents as `2.patch`. After that run `git apply --check 1.patch` and `git apply --check 2.patch`. If you don't get any errors when just run `git apply 1.patch` and then `git apply 2.patch` to patch your files with the changes. – Serban Constantin Feb 12 '15 at 18:12
  • Note `git apply` can also read from stdin. So one can issue the following command in the terminal: `curl -sL -o - https://github.com/org/repo/pull/92.patch | git apply -` – bric3 Dec 07 '22 at 10:42
0

You can fork the project and add the second one as second remote, Then you can merge the desired branches to your project.

git remote add remote2 git@github.com:niklasso/minisat.git git fetch remote2 git merge remote2 master

and then the updated code will be merged to your project. Once the pull request will be applied to the original repo (Merged pull request) you will not see any changes since you will already have the commit id in your copy.

prosseek
  • 182,215
  • 215
  • 566
  • 871
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

In addition to a command like git apply 1.patch mentioned in another answer, you can also do it with the patch command: patch -p1 < 1.patch.

There is also the gh command from GitHub CLI that can be used like gh pr checkout 1.

baptx
  • 3,428
  • 6
  • 33
  • 42