Search code examples
github-apigitlab-api

GitHub/GitLab REST API - Get diff between two branches


We want to get a difference between two GitLab/GitHub branches through REST API. We saw Git supports a command to do that but it seems they don't support for REST API. Is there any API support for this?

git diff --name-status firstbranch..yourBranchName

git diff --name-status origin/develop..origin/master

Showing which files have changed between two revisions


Solution

  • GitHub has dedicated URL (non-REST) for comparing branches.
    Example:

    https://github.com/octocat/linguist/compare/master...octocat:an-example-comparison-for-docs

    Same for GitLab:

    https://gitlab.com/gitlab-org/gitlab-foss/compare?from=master&to=master

    Although it can be different from a git diff.


    The REST API for GitHub would be: "Compare two commits"

    GET /repos/:owner/:repo/compare/:base...:head
    

    The response also includes details on the files that were changed between the two commits.
    This includes the status of the change (for example, if a file was added, removed, modified, or renamed), and details of the change itself.
    For example, files with a renamed status have a previous_filename field showing the previous filename of the file, and files with a modified status have a patch field showing the changes made to the file.

    For GitLab: compare branch API

    GET /projects/:id/repository/compare?from=master&to=feature