gitgithub

How can I generate a diff for a single file between two branches in github


I need to generate a diff for a single file that will show the differences between two versions, which are actually tags in github. I then want to send this diff to someone via email so a github URL for the diff would be ideal. The github compare view will allow me to do this for all changed files, but that's no good as there are thousands of files in my repo.

I can do this in the command line as follows, but this doesn't help as I need to send the diff to someone via email:

git diff tag1 tag2 -- path/to/file

I found the command line version discussed here: how can I see the differences in a designated file between a local branch and a remote branch?


Solution

  • GitHub only exposes the way to show diff between two commits.

    Provided those tags actually point to commits, the Url format would be something like

    https://github.com/{user}/{repository}/compare/{from-tag}...{until-tag}

    As an example, https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5 shows the diff between two versions of the LibGit2Sharp project. This diff includes all the modified files.

    If you want to retrieve a URL that targets a specific file:

    changed-tab

    show-diff

    For instance, given the diff above, the link https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5#diff-11 will point to the LazyFixtures.cs changes that occured between version v0.9.0 and v0.9.5.

    Update

    Following your comment which states that your diff is too big to be rendered through the Web interface, how about reverting to good old command line tooling? You could redirect the output of the diff to a file and then send the file as an email attachment.

    $ git diff v0.9.0 v0.9.5 -- LibGit2Sharp.Tests/LazyFixture.cs > /tmp/lazyfixture.diff