Skip to content

Exploring history

Overview

In this episode, we want to work with the version history and explain how you can restore older versions.

Tips for instructors

Checkpoints:

  • Please make sure that the participants could restore the last committed version of mars.txt at the end of this episode.

Working with the version history

Let us change the mars.txt again and see how we can compare a modification with different committed versions:

  • Edit the file mars.txt and change the following:
    • Add the following line: An ill-considered change
    • Please make sure that you added a closing blank line and saved the file properly.
  • You can compare the mars.txt with different versions using the HEAD pointer which references the most recent commit:
    • git diff HEAD mars.txt - Compares the mars.txt with the last committed version (represented by HEAD).
    • git diff HEAD~1 mars.txt - Compares the mars.txt with the second last committed version (represented by HEAD~1).
    • git diff HEAD~2 mars.txt - Compares the mars.txt with the third last committed version (represented by HEAD~2).
    • git diff HEAD~3 mars.txt - Tries to compare the mars.txt with the fourth last committed version (represented by HEAD~3). However, this attempt results in an error because such a commit does not exist.

HEAD~<NUMBER> allows you to navigate the commit history relatively starting from the most recent commit. In addition, you can use the commit ID directly to reference a specific version:

  • Determine the commit ID of the third last commit:
    • git show HEAD~2 mars.txt - Shows the “content” of the commit including the commit ID.
    • Alternatively, you can use git log --oneline to find out the commit ID.
  • Copy the commit ID. Normally, you only require the first 7 digits to uniquely identify a commit.
  • git diff <copied commit ID> mars.txt - Shows the same output as git diff HEAD~2 mars.txt

You can usually use a commit ID or a HEAD based expression, if a Git commands expects a reference to a commit.

Restore older versions

Imagine that we changed our mind about our last change and want to discard it:

  • git status - Shows that the mars.txt changed but these changes have not yet been staged. In addition, the output indicates that we can use git restore to discard the changes.
  • git restore mars.txt - Discards the changes and restores mars.txt to the content of the last commit.
  • Let us check the restored the version:
    • cat mars.txt - Shows that mars.txt is back to last committed version.
    • git status - Indicates that there no modifications in the repository.

Now, we want to restore an even older version of mars.txt:

  • git restore -s HEAD~2 mars.txt - Restores the mars.txt content of the initial commit.
  • cat mars.txt - Shows the content of the initial commit.
  • git status - Shows that mars.txt has been modified.

Finally, we want to restore the last committed version again:

  • git restore mars.txt - Restores the last committed version of mars.txt.
  • cat mars.txt - Shows the content of the last commit.
  • git status - Shows no more modifications.

Discard local changes

The commands git restore <filename> or git restore -s <commit ID> <filename> might permanently remove your local changes without the chance to undo this.

Discard staged changes

You can use git restore -S <file> or git restore --staged <file> to remove changes from the staging area. Afterwards, the changes are still there but no longer staged for the next commit. In the next step, you could discard the changes entirely by using git restore <file>.

Key points

  • You can reference a commit by its commit ID or relatively via HEAD pointer expression.
  • You can use git restore to discard (staged) changes and restore the content of older versions.