Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a Git object from getting garbage collected?

From what I understood any object gets garbage collected when they have no refs. What is the best way to prevent collection of objects that we want to persist in the database?

A use case is when in a pull request one makes changes (maybe according to a code review) and previous commits become detached, they are not going to be merged in the repository but they should always be available in order to allow tracing of the changes in the pull request.

Example:

  • CommitA fixes a bug
  • Create a pull-request for it
  • Somebody reviews and suggests a change, linking to a specific line in the code
  • Change code, amend CommitA and re-commit as CommitA2

Now CommitA2 is what will be in the change history, but the pull-request will still have a link pointing to the old CommitA. In some years we want to be able to see what the pull-request was about and what its comments were referring to.

How does one prevent the commit from being collected by GC?

Give it a tag is the first solution that comes to my mind.

like image 657
Kamafeather Avatar asked Feb 03 '26 16:02

Kamafeather


1 Answers

Refs don't have to be branches or tags, you can keep local refs to anything you want.

Here's a simple "make me another snapshot ref for pull 137",

next=$((`git rev-list --no-walk --count --glob=refs/snap/pull/137/head-v*`+1))
git update-ref refs/snap/pull/137/head-v$next refs/pull/137/head
like image 165
jthill Avatar answered Feb 06 '26 07:02

jthill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!