This lesson is being piloted (Beta version)

Undoing things

Overview

Teaching: 10 min
Exercises: 15 min
Questions
  • How can I undo things?

Objectives
  • Learn to undo changes safely

  • See when changes are permanently deleted and when they can be retrieved

Undoing things

The whole point of version control is that you can be see and retrieve previous snapshots of your work.

With Git, if you have committed (made a snapshot) of a file, you can get it back, even if it gets deleted or modified in the future.

Some commands will modify the hitory of a Git repository. This is generally a very bad idea, and you should only do it if you’re really confident you know what you’re doing.

If changes are uncommitted, they are not safe, and if they are deleted, they are gone.


Reverting commits

$ git log --oneline

f960dd3 (HEAD -> main) not sure this is a good idea
40fbb90 draft a readme
dd4472c we should not forget to enjoy
2bb9bb4 add half an onion
2d79e7e adding ingredients and instructions

A safe way to undo the commit is to revert the commit with git revert:

$ git revert f960dd3

This creates a new commit that does the opposite of the reverted commit. The old commit remains in the history:

$ git log --oneline

d62ad3e (HEAD -> main) Revert "not sure this is a good idea"
f960dd3 not sure this is a good idea
40fbb90 draft a readme
dd4472c we should not forget to enjoy
2bb9bb4 add half an onion
2d79e7e adding ingredients and instructions

Challenge 1

Make a new commit in your guacamole repo (it can be whatever you like)

Inspect the history, and revert the commit.


Undo unstaged/uncommitted changes

git restore is used to restore a file, or all files, back to a previously commited state.

Note: Older versions of git used git checkout <file> instead of restore.

DANGER!!

This command permanently deletes any changes that haven’t been staged/committed!

Modify before staging

Modify after staging


Challenge 2

How much do you trust Git…?

Delete one (or more) of your committed files using any method you like.

Can you get it/them back?

Solution to Challenge 2

As long as a file was committed, you can get it back with git restore <file>, but Git isn’t magic - you won’t have any changes that weren’t committed.

Recovering a previous versions

Because git stores the complete history of whatever snapshots you have recorded, you can step back to anyone of them at different levels of detail, from the complete working directory, to single files, even to single changes within files.

To get back to a previous state for the whole working directory, you can use git restore [-s <commit>] .. Jumping back to a previous commit can be useful for having a look at files, but you may get a warning about a detached HEAD (more discussion later).

Recovering previous versions of single files

Often it’s useful to be able to access a previous version of a particular file. When the restore command is given a file path (along with a reference to a commit with “-s” or “–source=”) it will update that path to the previous state.

Let’s see that with our recipe. If we wanted to get ingredients.txt back to its state before the addition of the onion, we could run

git restore -s 2d79e7e ingredients.txt

Updated 1 path from 2d79e7e

If you run git status you’ll see that the changes to the file ingredients.txt, bringing it back to the previous state, are already staged and ready to be committed.

Key Points

  • Git history can be reverted without modifying it

  • Once changes are committed they are safe

  • Changes that are not committed can be deleted