Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

gitlab

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

like image 983
yigal Avatar asked Apr 27 '18 19:04

yigal


People also ask

How do you compare the differences between two commits?

The git diff command is commonly used to get the unstaged changes between the index and working directory. It can be also used to show changes between two arbitrary commits. To view the changes between two commits, you can provide the commit hashes.

Can we compare two commits in git?

To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..

How do I compare two commit codes in git?

You can also compare two arbitrary commits in your repository or its forks on GitHub in a two-dot diff comparison. To quickly compare two commits or Git Object IDs (OIDs) directly with each other in a two-dot diff comparison on GitHub, edit the URL of your repository's "Comparing changes" page.

How do I compare two versions of files in git?

You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch. Let's compare two commits in our Git repository.


2 Answers

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'
like image 68
robertbeb Avatar answered Sep 22 '22 05:09

robertbeb


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.

like image 39
anapsix Avatar answered Sep 19 '22 05:09

anapsix