>_ IzzyLab

Hands-on task

This task is step 15 of the course “Git: rewriting and rescuing history”

Recover a lost commit via reflog

hard~12 min

The project repo lives in GitLab and is cloned to /root/repo (the web sign-in is on the GitLab tab). The develop branch had a commit add important with the file important.txt. By mistake you ran git reset --hard HEAD~1 and git push --force: the commit is gone both from your local develop and from GitLab.

Restore that very lost commit: the develop branch must point at it again — both locally and in GitLab, important.txt must come back with its old contents, and develop's history must have two commits. Find the lost commit through the reference log.

What you'll practise:
  • git reflog
  • recovering commits
  • reset --hard
  • HEAD@{n}
  • force push
  • GitLab

Topic breakdown

When a branch stops pointing at a commit (for example, after a hard reset), the commit object itself isn't erased immediately — it becomes "dangling" and lives in the database for a while until garbage collection picks it up. Git records every move of HEAD in a separate reference log, read via git reflog; from it you can find the hash of the lost commit and point a branch back at it. This is a lifeline after mistaken reset, rebase, or branch deletions.

What's checked

  1. important.txt is restored locally
  2. develop points at the lost commit
  3. important.txt is back in GitLab
  4. develop in GitLab is that very commit

What it looks like