42

I have forked a project on github and need to have a set of changes I made since I forked, in diff format.

If you wonder - I've forked Apache httpd and I'm changing some code in core. Currently I'm not commiting any changes, running git diff, and use its output as a patch against vanilla httpd sources in an RPM building process. It is, of course, wrong, but I don't know how to do it properly. All I know is I need a diff in the end.

GDR
  • 2,301
  • 1
  • 21
  • 26

4 Answers4

41

Online solution:

get /repos/{owner}/{repo}/compare/{base}...{head}

The "compare two commits" API does support multiple repositories:

Both :base and :head must be branch names in :repo.
To compare branches across other repositories in the same network as :repo, use the format <USERNAME>:branch.

Example:

https://api.github.com/repos/octocat/hello-world/compare/master...abejpn:master

Or with a GitHub URL:

https://github.com/octocat/Hello-World/compare/master...abejpn:master


Original answer 2010:

  • Add the original GitHub repo (the one you have forked) as a remote one on your local repo.
    (git remote add mainRepo github_url)
  • git fetch mainRepo to get the latest changes from that original "mainRepo".
  • git log HEAD..mainRepo/master will show you all your changes between the latest on mainRepo master branch and your current branch.
    git diff HEAD..mainRepo/master would display it in diff format.

In learn.GitHub:

git diff mainRepo/master...HEAD

would list all your changes since you have forked from mainRepo:

This will not compare the last ‘master’ branch snapshot and the last ‘dev’ snapshot - it will instead compare the common ancestor of both with ‘dev’. That will tell you what changed since the branch point.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you very much, this solves my issue. I definitely need to spend some time to understand git philosophy deeper. – GDR Sep 27 '10 at 09:42
  • +1. If I may add two notes: I believe the HEAD parts are unnecessary if you are currently on the head. Also, `git difftool mainrepo/master` and `git difftool ..mainrepo/master` are useful if you want to view the changes in your configured difftool. – Peter Majeed Jan 11 '13 at 16:49
  • How to view diffs of a forked project via TortoiseGit - http://stackoverflow.com/a/7297491/968003 – Alex Klaus Dec 28 '16 at 05:07
  • *`fatal: ambiguous argument 'HEAD..upstream/master'`* and *`fatal: ambiguous argument 'HEAD...upstream/master'`*... Reversing arguments made no difference. Yet another simple task made difficult. – jww Apr 17 '17 at 13:26
  • @jww do you have defined upstream in your remotes (`git remote -v`)? DO you have created a branch named `upstream/master`? (http://stackoverflow.com/a/26046933/6309), or a tag (http://stackoverflow.com/a/35980056/6309)? – VonC Apr 17 '17 at 16:10
15

This is an old question, but I just found a very nice method to get a patch or diff file directly from Github.

When you are on your fork, there is a "Compare" link. Using that you are getting to the compare view.

Example

https://github.com/luisgoncalves/xades4j/compare/master...beat2:master

Now you can manually add either ".diff" or ".patch" to the end of this url, and you get the file directly in your browser.

Example

https://github.com/luisgoncalves/xades4j/compare/master...beat2:master.diff

Source: https://github.com/blog/967-github-secrets

beat
  • 1,857
  • 1
  • 22
  • 36
7

If you push a branch that tracks the "upstream" repo to your repository, then you can see the diff in github itself, too:

 git remote add mainRepo github_url
 git fetch mainRepo
 git branch main_repo_master mainRepo/master
 git push origin main_repo_master

Then see it online like this:

https://github.com/rdp/mplayer-svn/compare/master…main_repo_master

ref: http://betterlogic.com/roger/2012/04/github-compare-commits

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
2

Get parent/fork point sha1: git merge-base master HEAD Get diff: git diff <sha1>

Or in one command: git difftool $(git merge-base master HEAD)

Which is the same as sugar command: git diff master...HEAD

AuthorProxy
  • 7,946
  • 3
  • 27
  • 40