On GitLab, how to compare a file of two different commits

On GitLab, how to compare a file of two different commits? I know that that on command line git, the command is:

git diff commit1 commit12 -- file_name

What is the link format to do this on GitLab?

See my related question


It will compare commits, you will be able to find file in list.

  1. go to Repository > Compare
  2. paste this url: https://gitlab.com/$USER/$REPO/compare?from=$SHA1&to=$SHA2
  3. hit enter (notice: gitlab will set 'Source' and 'Target' properly)
  4. click button 'compare'

It appears the direct URL for compare is as follows

# compare URL, where ref_source and ref_target can be commit SHA, tag, or branch
https://${gitlab_host}/${repo_path}/-/compare/${ref_target}...${ref_source}

# tag example 1, comparing tag v1.5.1 to master
https://${gitlab_host}/${repo_path}/-/compare/v1.5.1...master

# tag example 2, comparing tag v1.5.1 to tag v1.5.2
https://${gitlab_host}/${repo_path}/-/compare/v1.5.1...v1.5.2

# commit example 1, comparing commit SHA to master
https://${gitlab_host}/${repo_path}/-/compare/f6098082f...master

# commit example 2, comparing commit SHA to another commit SHA
https://${gitlab_host}/${repo_path}/-/compare/f6098082f...2b8daf28

To compare a single file across two commits, one needs to get a blob id of the file first, and append it to the compare url following a octothorp (#); gotta find a way to get that id though

# compare URL, where ref_source and ref_target can be commit SHA, tag, or branch, and file_blob
https://${gitlab_host}/${repo_path}/-/compare/${ref_target}...${ref_source}#${file_blob}

And the compare UI can be reached at https://${gitlab_host}/${repo_path}/-/compare, where you can select source and target refs via drop-down.


None of the other answers had the correct steps to show a diff between two branches via the Gitlab GUI in its current version. To do so:

  1. Go to your project
  2. Hover over "Repository" in the side-menu and click "Branches"
  3. Next to the branch you wish to compare, click the "Compare" button on the right.
  4. You can then change the target branch from master (or whatever your default branch is called) if you wish.
  5. At the top is commits. Scroll down to see the actual diffs for each modified file.
  6. Scroll to the specific file you want to compare and click to expand if necessary.

You can open two tabs:

  1. go the list of commits (in the sidebar: Repository -> Commits), from where you have the SHA of any given commit (there's the copy button on right) handy;
  2. go the the compare tool (in the sidebar: Repository -> Compare), and at the top, in source and target, past the SHA strings of the two commits you want to compare.