>_ IzzyLab

Hands-on task

This task is step 13 of the course “Everyday Git”

Hard reset to the desired commit

medium~5 min

In the repository /root/repo there are three commits: init (file file1.txt), second (file file2.txt) and third (file file3.txt). Completely discard the last two commits: the main branch must point at the init commit again, and the files file2.txt and file3.txt must disappear from the working tree. The working tree must be left clean.

What you'll practise:
  • git
  • git reset
  • --hard
  • working tree
  • HEAD~2

Topic breakdown

To throw out both commits and the files associated with them from the working tree, you need the harshest git reset mode — the one that syncs both the index and the working tree to the target commit at once. It irreversibly destroys unsaved edits, so apply it carefully. The target commit is specified with a relative reference (several steps back from HEAD) or its hash from git log --oneline.

What's checked

  1. one commit remains
  2. extra files removed
  3. working tree is clean

What it looks like