>_ IzzyLab

Hands-on task

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

Restore a file to its version from history

easy~2 min

In the repository /root/repo the file file.txt was committed with the line good, but then it got accidentally messed up — right now the working tree holds bad (the change is not staged). Restore file.txt to the last committed version: the content should become good again, and the working tree should be clean.

What you'll practise:
  • git restore
  • git checkout --
  • working tree
  • discarding edits
  • HEAD

Topic breakdown

Uncommitted edits in the working tree can always be rolled back by taking the file's version from the last commit — conceptually this is "re-laying down" the file from HEAD. The modern interface for this is git restore <file>, and the classic equivalent is git checkout -- <file>. The key nuance: you should restore a single file by name, not the whole repository, so you don't lose other changes.

What's checked

  1. content restored
  2. working tree is clean

What it looks like