>_ IzzyLab

Hands-on task

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

Restore a file in both the index and the working tree

medium~5 min

In /root/repo the file f.txt in the last commit contains v1. Then it was changed to v2 and staged (git add), and after that the working copy was changed once more to v3 (without staging). Right now three versions have diverged: the commit is v1, the index is v2, the working tree is v3. Return f.txt fully to the version from the last commit: both the index and the working tree should be v1 again. Don't touch the commit history.

What you'll practise:
  • git restore
  • index
  • working tree
  • staging area
  • reverting a file

Topic breakdown

In git a file has three states: the version in the last commit (HEAD), the version in the index (staging area), and the version in the working tree — and they can diverge. git restore can restore a file in both working areas, but by default it touches only the working copy, taking the contents from the index. To reset both the index and the working tree to the version from the commit at once, the command has two separate scope flags that specify exactly where to write; they are applied together. restore doesn't touch the commit history in the process.

What's checked

  1. index = v1
  2. working tree = v1
  3. history intact

What it looks like