yigal
yigal

Reputation: 4745

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

Upvotes: 149

Views: 197192

Answers (10)

some user
some user

Reputation: 1008

I cannot get the "compare" URL to work. Here is a hack to make it possible to compare a file against a different version.

  1. Go to the file view on one of the commits. On the file's menu bar, next to the "Download" icon, there is the "Open raw" button. Click on it and opens the raw file. Select All and Copy.

  2. Go to the file view of the other commit you want to compare. On the file's menu bar, click on the "Edit" button and select "Edit single file" (you may need to fork the project or create a private branch if it is locked).

  3. Paste over the raw file and click on "Preview changes"

It should show you the changes, which is the diff between the 2 versions.

Upvotes: 0

Gabriel Lopez
Gabriel Lopez

Reputation: 47

It's in the menu:

enter image description here

There is a tool to compare side-by-side

Upvotes: 0

robertbeb
robertbeb

Reputation: 2010

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 or https://your gitlab domain/$ORG/$GROUP/$REPO/-/compare?from=$SHA1&to=$SHA2
  3. hit enter (notice: gitlab will set 'Source' and 'Target' properly)
  4. click button 'compare'

Upvotes: 176

The Bndr
The Bndr

Reputation: 13394

As addition to the answer from @anapsix by using this url:

https://${gitlab_host}/${repo_path}/-/compare/${ref_target}...${ref_source}

That solution still works, but ${ref_target} and ${ref_source} are not the commit SHA key. You will need the short version of that key: only the first 8-chracters.

Upvotes: 2

anapsix
anapsix

Reputation: 2095

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.

Upvotes: 33

Gus Lopez
Gus Lopez

Reputation: 353

You can accomplish this with simple point-and-click through the GitLab web interface, provided the code can be viewed on the screen.

  • On your repository, click on the left Menu option "Commits". The right pane will show a list of all the commit events in the repository.
  • Click on the one that corresponds to the file you're interested in. You will be able to see its "before and after" state.
  • You can see it in "inline" or "side-by-side" mode, where it highlights changes in green.

Upvotes: -3

Use Me
Use Me

Reputation: 483

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.

Upvotes: 8

twhitney
twhitney

Reputation: 325

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.

Upvotes: 17

singuliere
singuliere

Reputation: 857

You first need to get the sha1 digest of the file path you are interested in (for instance with the sha1sum command). You can then build the URL to display the diff between two git ref (i.e. branch names or commit hash or tags) as follows:

https://gitlab.com/<project>/-/compare/<ref1>...<ref2>#<sha1>

Here is an example with the AN/R5/L15/S2018/IDS/000/020/RUANR5L15S2018IDS20864.json filename. Its sha1 is f88a5e03ecc7854e8955927af33f2ea9d090ddaf:

$ echo -n AN/R5/L15/S2018/IDS/000/020/RUANR5L15S2018IDS20864.json | sha1sum
f88a5e03ecc7854e8955927af33f2ea9d090ddaf

Comparing it between the 878bd4f4f7edbabad3c691a7df3e317348d4ca77 and 59e63f529a7fe5f28131dec36f253dca46b8ba9c commits is done with the URL: https://git.en-root.org/tricoteuses/data.tricoteuses.fr/Agenda_XV/-/compare/59e63f529a7fe5f28131dec36f253dca46b8ba9c...878bd4f4f7edbabad3c691a7df3e317348d4ca77#f88a5e03ecc7854e8955927af33f2ea9d090ddaf

The page will also display other diffs, not just the one you are interested in. But it will move you right where the diff of the file starts and you can ignore the rest.

There are a few GitLab feature requests to make it easier for the user to discover this in the future.

Upvotes: 6

david zhang
david zhang

Reputation: 17

if you want to compare one file different version

1.Repositoy->Commits->select file->click Side-by-side

2.you will see two windows,left is old version and right is new version.

Upvotes: -1

Related Questions