gitgitlabsquash

How to checkout to a commit after it gets squashed?


During merge I use both Delete source branch when merge request is accepted and Squash commits when merge request is accepted options.

Both options are enabled

Gitlab does his job perfectly: It squash commits to the single one and merge it to the target branch.

Squashing works as expected

But it also keeps original commits - you can see them in Commits tab of merge request page:

The "Commits" tab

I want to be able to checkout to these commits. But in new cloned repository, git can't find them by hash:

$ git checkout c8ad8edf
error: pathspec 'c8ad8edf' did not match any file(s) known to git

I think this happens because git clone gets only commits that can be reach from the initial commit. But GitLab stores this information - I can view info of any commit in browser:

Unreachable commits can be viewed using GitLab web interface

So how can I checkout to these commits?


Solution

  • You can fetch the refs/merge-requests/$id/head ref, where $id is the numerical ID of the merge request (3 in your case, from your screenshots): see Checkout merge requests locally through the head ref. Note that in GitLab 13.4 and newer, that ref is deleted 14 days after the MR is merged or closed.

    git fetch origin refs/merge-requests/3/head
    git checkout FETCH_HEAD