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

前端 未结 5 1782
有刺的猬
有刺的猬 2021-01-30 00:30

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

5条回答
  •  悲哀的现实
    2021-01-30 00:55

    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
    # git@github.com: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.

提交回复
热议问题