Given the following situation:
- A local git branch has been force-pushed to the server, or the server's previous version of that branch was deleted or otherwise lost.
- There was one commit present on the server that had not been fetched locally.
- The SHA1 of that "lost" commit is known, and it has not yet been GC'd on the server, but no server-side refs currently point to it or any descendants of it.
(How this situation was reached is irrelevant.)
Is there anything that can be done purely client-side to "recover" this commit? eg. remotely create a remote branch that points at it, or fetch it as a local unreachable object that can then have a local branch pointed at it?
Do the answers change if it's possible to set some server-side config options in advance of this event happening? (I know Git normally tries to not let you fetch unreachable commits, so part of the question is whether there's a way to disable that.)
(FWIW, I've already recovered from this by directly creating a local branch on the server, which then allows it to be fetched locally as a remote branch and recovered as desired. But I'm curious how this situation could be solved if server access hadn't been available. Since GC might destroy unreachable commits at any time, this is time-sensitive and "wait until you can ask server admins to recover it for you" is not a valid solution.)
On a peripherally related note, why is the "safe" option --force-with-lease
longer to type than the unsafe option --force
(or even -f
)?
Some old answers on this site and elsewhere suggest that it's possible to use one of these:
git fetch origin SHA1
git fetch origin SHA1:refs/heads/temp
git fetch origin SHA1:refs/remotes/origin/temp
However at least with Git 2.5.3 none of these appear to work with an unreachable SHA1 -- they just silently do nothing.