CS FundamentalsHardProduct-company placement · Git interview

On main with tip D (history A-B-C-D) you accidentally run git reset --hard HEAD~2. Which command brings the branch back to D, assuming nothing else has been done?

  1. A. git revert HEAD~2
  2. B. git checkout HEAD~2
  3. C. git reset --hard HEAD@{1}
  4. D. git pull --rebase

Correct answer: C. git reset --hard HEAD@{1}

The reflog records where HEAD was: HEAD@{0} is now B and HEAD@{1} is D, the position before the reset. Resetting to it recovers the 'lost' commits, which still exist until garbage collection.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic