This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: An army of robots

Viewing git snapshots

face Josiah Wang

Now, you would normally checkout a branch in Git.

user@MACHINE:~/robot$ git checkout refactor
Switched to branch 'refactor'
user@MACHINE:~/robot$ git checkout main
Switched to branch 'main'

You can actually also checkout a commit. Let’s say we want to go back in time to before we generated multiple robots earlier. Find out the commit hash for that particular commit (the short one is sufficient). It is f8a6583 in my example.

user@MACHINE:~/robot$ git log --oneline -5
1aecbb6 (HEAD -> main) The program now generates a unique ID for each robot.
a12bc76 The program now generates multiple robots, each navigating to a different target cell.
f8a6583 Updated main.py to randomly pick a name for the robot from a pre-defined list. Also added robot_names.txt that contains the list of candidate names for the robot
dec3cd6 (refactor) Refactored code to be modular and self-explanatory.
0ebdde4 Improved robot in main.py so that the robot automatically navigates to cell (9,9) for its Ribena.

Now, run git checkout with your commit hash.

user@MACHINE:~/robot$ git checkout f8a6583
Note: switching to 'f8a6583'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at f8a6583 Updated main.py to randomly pick a name for the robot from a pre-defined list. Also added robot_names.txt that contains the list of candidate names for the robot

Then look at your main.py. You should see your old code with your single lonely robot before you gave it a few more friends. This is how you can look at previous versions of your files, if ever needed.

Detached HEAD

You might also have noticed the long warning from Git about you being in a ‘detached HEAD‘ state.

Git is basically warning you that your HEAD pointer now no longer points to a branch (i.e. the commit does not have a name).

Detached HEAD

The reason for this warning is that you might decide to update the current file and commit it to a repo.

Making a commit from a detached HEAD

Remember that you are no longer on a branch (with a name)! So unless you jot down the commit hash somewhere, you will not be able to reach this commit in the future once you switch back to a branch, say the ‘main’ branch. All other branches will have no knowledge about this particular commit, so it will be lost!

Unreachable commit

If you do intend to keep this (maybe you also want a version of robot who remains a loner), then the proper way would be to first create a new branch from the detached HEAD and then checkout the branch. Then you can commit and come back to the branch at any point after that.

In any case, I recommend checking out a commit in a detached HEAD state only for read-only purposes. And then git checkout main when you are done.

With that warning, go ahead and stroll down your memory lane, and look at old versions of your robot project. Reminisce about how far you have come since the very first version of your robot project! Remember to git checkout main to go back to the latest commit once you are done!

user@MACHINE:~/robot$ git checkout main
Previous HEAD position was f8a6583 Updated main.py to randomly pick a name for the robot from a pre-defined list. Also added robot_names.txt that contains the list of candidate names for the robot
Switched to branch 'main'