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

GitGithub

Git Problem Overview


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: https://stackoverflow.com/questions/10611112/how-can-i-see-the-differences-in-a-designated-file-between-a-local-branch-and-a

Git Solutions


Solution 1 - Git

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:

  • Switch to the Files Changed tab

changed-tab

  • Click on the Show Diff Stats button (This will display the list of modified files as links)

show-diff

  • Copy to the clipboard the link of the specific file you're after... and Tada! You're done.

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

Solution 2 - Git

Here is my workaround when the following issue applies.

> This comparison is big! We’re only showing the most recent 250 commits

Copy the raw view of the file that you want to compare to https://gist.github.com/. Use the two specific commit points that you want to compare. Start with the older commit.

https://gist.github.com/ has a nice side-by-side diff view when you click 'Revisions'.

Solution 3 - Git

Answer is for people who want to only see (Not to download) the history/reviosion of code changes of a file in the GITHUB WEB Page for previous checkin.

Go to that file in the github, then select HISTORY. This will open page with list of checkin comments link like below.

enter image description here On clicking on it will show the code changes. After clicking the history; you can click on packages to see package level all files checkins.

In eclipse you can compare the history using EGit plugin and "Right click ->Compare with" on the file. https://stackoverflow.com/questions/1396649/how-can-i-compare-two-revisions-in-git-in-eclipse

Solution 4 - Git

I used nulltoken's answer to put together a simple convenience script for pulling up a diff between two commits on GitHub from the command line.

You can find the full script on gist, but here are the good bits:

# Parse the following patterns for repo urls to get the github repo url
# https://github.com/owner/repo-name.git
# [email protected]:owner/repo-name.git
BASE_URL="https://github.com/""$(git config --get remote.origin.url | sed 's/.*github\.com[/:]\(.*\).git/\1/')""/compare"

if [[ "$#" -eq 1 ]]; then
  if [[ "$1" =~ .*\.\..* ]]; then
    # Handle "git hubdiff fromcommit..tocommit"
    open "${BASE_URL}/$(git rev-parse "${1/\.\.*/}")...$(git rev-parse ${1/*\.\./})"
  else
    # Handle "git hubdiff fromcommit"
    open "${BASE_URL}/$(git rev-parse "$1")...$(git rev-parse HEAD)"
  fi
elif [[ "$#" -eq 2 ]]; then
  # Handle "git hubdiff fromcommit tocommit"
  open "${BASE_URL}/$(git rev-parse "$1")...$(git rev-parse "$2")"
fi

It accepts as arguments branches, commits, and anything else that can be resolved by git rev-parse. I used open, which only works on macOS for opening webpages, so if you're on a different environment you'll want to tweak that.

As with nulltoken's answer, in order to point to a single file in the diff, you'll have to click on the file's title to make the anchor string appear in the url bar, which you can then copy.

Solution 5 - Git

Since this still isn't possible here's a browser-based diff-tool method. It does not leverage automation but only requires the ability to install Chrome extensions:

  1. Install Diff Tools for Chrome browsers: https://chrome.google.com/webstore/detail/diff-tools-text-pdf-doc-o/lkcdojpmjehlniamnglpjlldkoonlomb
  2. Open Diff Tools ( http://iblogbox.com/devtools/diff/ )
  3. From GitHub go to the BEFORE commit, tag, or branch, open the file, then click on the Raw button to get the raw file view, select-all and copy, then put in the left-hand side text box in Diff Tools
  4. Repeat step 3 but for the AFTER file and paste into the right-hand-side box in Diff Tools
  5. Click Compare Now and perform your diff ad-hoc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionplainflavourView Question on Stackoverflow
Solution 1 - GitnulltokenView Answer on Stackoverflow
Solution 2 - Gitf01View Answer on Stackoverflow
Solution 3 - GitKanagavelu SugumarView Answer on Stackoverflow
Solution 4 - GitChrisView Answer on Stackoverflow
Solution 5 - GitkayleeFrye_onDeckView Answer on Stackoverflow