Git shows weird behavior when being runned from opam

By some reason I can’t install pinned packages.

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> retrieved merlin.4.3.1-412  (no changes)
+ /media/mand/.opam/opam-init/hooks/sandbox.sh "build" "dune" "subst" (CWD=/media/mand/.opam/4.12.1+flambda/.opam-switch/build/merlin.4.3.1-412)
-          git (internal) (exit 128)
- /usr/bin/git describe --always --dirty > /opam-tmp/dune4756a7.output
- error: object directory /media/mand/.opam/download-cache/git/objects does not exist; check .git/objects/info/alternates
- fatal: bad object HEAD
-          git (internal) (exit 128)
- /usr/bin/git ls-tree -z -r --name-only HEAD > /opam-tmp/dune64cebd.output
- error: object directory /media/mand/.opam/download-cache/git/objects does not exist; check .git/objects/info/alternates
- fatal: not a tree object
[ERROR] The compilation of merlin.4.3.1-412 failed at "dune subst".

I found something similar in `opam clean -c` seems incompatible with `opam pin add -k git (...)` · Issue #3828 · ocaml/opam · GitHub and tried

  • opam clean -c
  • (cd /media/mand/.opam/4.12.1+flambda/.opam-switch/build/merlin.4.3.1-412 && git gc).
  • (cd /media/mand/.opam/4.12.1+flambda/.opam-switch/build/merlin.4.3.1-412 && git fsck --full)

But these command didn’t help me.

Any advice how to fix this? How to make this issue never appear?