Let's take these in order:
Is this operation secure against data loss? Is there a chance that his commit is lost?
No and no, in that order. Or maybe "not yet" is better for the second answer.
Git's reset --hard
adjusts (writes to) three things: the recorded branch tip, which changes from wherever it is now, to the argument commit; the index, which changes to match the new current commit once the branch is updated; and the work-tree, which changes to match the index once the index is updated.
Some of these writes are completely unrecoverable, some are somewhat recoverable with difficulty, and some are easily recoverable. In particular, Git itself saves nothing of the work-tree, so these are unrecoverable (except by means outside Git, e.g., file system snapshot / backup).
You can remove the "write to work tree" aspect of git reset
by using git reset --mixed
. That still writes to the index. Because the index contains only metadata, some file contents (if lost in this phase) can be retrieved. How hard this is varies, but it's generally no fun. The metadata are of course gone (except by means outside Git, again: the index is by default stored in one or sometimes two files within the .git
directory, or the per-worktree area for git worktree add
-ed secondary worktrees).
You can even remove the "write to index" aspect of git reset
by using git reset --soft
. That writes only the recorded branch-tip. This is the one that is easy to recover, at least for a short while, as the previous branch-tip value is immediately saved in ORIG_HEAD
.
The ORIG_HEAD
name does, of course, get overwritten by another git reset
, so if you do not save it quickly enough, that might lose it. However, there is a second mechanism by which all previous values of all references—both HEAD
itself, and the branch name—are saved for at least 30 days by default, in Git's reflogs. So even if you lose ORIG_HEAD
, you still have some reflog entries, unless you have turned off reflogs.
(Reflogs are off by default in new --bare
repositories, but on by default in all others.)
Wouldn't this operation leave a dangling reference to his commit that can be removed by runnning git gc?
If it were not for the reflogs and the saved ORIG_HEAD
, yes. Those count against gc
though. As long as ORIG_HEAD
or a reflog entry (or both) protects any given commit, that commit will remain in the repository, along with everything reachable through that commit.
Is there a way to recover the commit if the [noted commit ID] is lost?
Reflogs (and ORIG_HEAD
) are the usual way. Should those be lost as well, git fsck --lost-found
finds unreachable commits (and other unreachable objects) and restores them into the lost-found
subdirectory within .git
, assuming they have not been git gc
-ed. This is also the way to find modified files that were git add
-ed but never committed (they become "dangling blobs" and --lost-found
resurrects them).
All that said, git reset
is the wrong way to do this
The right way to look at old commits is indeed to just use git checkout
to extract them. You will get a "detached HEAD", but that's normal enough while looking around: just git checkout <branch>
to re-attach your HEAD later. Or, if you're trying to track down the point where a bug was introduced, use git bisect
, which repeatedly checks out old commits as you narrow in on the problem. With git bisect
, you identify some (earlier) commit where things are good and some (later) commit where things are bad, and then it picks a commit about halfway between for you to test. You then test it, declare it good or bad, and bisect picks the next one that's the next half-way.
If you have an automated test, that's even better: you can git bisect run
your automated test, and let that do all the work. But even if not you can still git bisect
manually, as long as you have a way to declare any given commit "good" or "bad".