Are commits preserved by GitLab even after the branch is deleted after MR get merged

693 Views Asked by At

In our development using GitLab we usually squash commits from feature branches when they are merged in main branch (we use rebase strategy instead of merge if it matters).

But if the commits from feature branch are squashed to a single commit (which is later merged in main branch) and the branch itself is deleted, how can GitLab still show the original commits? Are they kept in the repository forever or it is a matter of time when they finally get dropped?

I believe GitLab does some garbage collection on the repository, but I wonder why it does not affect commits from MR.

2

There are 2 best solutions below

0
On

The squash and merge commit page does mention:

Each time a branch merges into your base branch, up to two commits are added:

  • The single commit created by squashing the commits from the branch.
  • A merge commit, unless you have enabled fast-forward merges in your project.
    Fast-forward merges disable merge commits

That merge commit might very well keep a reference to the branch before squashing, which can explain why the original commits are still visible.

If that is not the case, meaning nothing actually references those commits, then housekeeping (described here for self-managed GitLab, but also active on Cloud offering) will remove them the repository eventually.
However:

  • Gitaly cannot ever delete unreachable objects from object pools because they might be used by any of the forks that are connected to it.
  • Gitaly must keep all objects reachable due to the same reason. Object pools thus maintain references to unreachable “dangling” objects so that they don’t ever get deleted.
2
On

In case of squashing, the feature branch commits are squashed into a single commit to be pushed to the target branch. On the target branch, you will see only a single commit. See this image to see the commit on the target branch i.e. vighnesh_develop

But you will still be able to see the individual commits on the merge request from the deleted feature branch. The feature branch is deleted but all the merge requests are maintained for lifetime unless we manually set an expiration period. See this merge request for squashed commit

Hence, the individual commits are linked to the merge requests and not to the deleted branch or the target branch. Git will maintain a list of ALL the commits even though the branch from which the commit came is deleted. That is how it is able to do effective version control.