Advanced git

Block 1.2: History & Time-Travel

Jan Simson

Seeing History: git log

We can see the history of our committed changes, called commits using git log.

git log
# commit ed226e022f60d9f578265c2c246367f5f07756de (HEAD -> main)
# Author: Jan Simson <git@simson.io>
# Date:   Fri Sep 16 17:00:03 2022 +0200

#     Edit hello.txt

# commit 795780d123f6eeedaa09734005c08d1ad89c1976
# Author: Jan Simson <git@simson.io>
# Date:   Fri Sep 16 16:59:45 2022 +0200

# Adding hello.txt

# lines 1-9

You can move up and down with the arrow keys and leave the log view by pressing q.

Properly Traveling Through Time:
git checkout

Background: https://giphy.com/gifs/back-to-the-future-xsF1FSDbjguis

Living History: git checkout

You can go back to any previous commit (and many other things!) with git checkout.

# Full hash
git checkout 795780d123f6eeedaa09734005c08d1ad89c1976
# Short hash
git checkout 795780d
# Relative
git checkout HEAD~2

Going Meta: git reflog

Git tracks where your HEAD has been and what you do in your repository. This history can be seen via git reflog.

  • This includes moving around via git checkout
  • Local to your computer (not synched via push / pull)
  • Mini-Practical: Try it out in your current repository

Practical: Murder Mystery (Setup) 👮

  1. Create a new VS Code / Terminal window
  2. Clone the git-murder-mystery:

git clone https://github.com/jansim/git-murder-mystery.git

Practical: git Murder Mystery đŸ•ĩī¸

  1. Unravel the murder case.
    • Use git log to see the history of the repository
    • Use git checkout to go to the past
  2. There are four suspects:
    • butler 🛎ī¸
    • gardener đŸŒŗ
    • barber ✂ī¸
    • carpenter đŸĒ“
  3. Look at the git reflog after you solved the case

End of Section 🎉

Any Questions?

[🏡 Back to Overview]

[⏊ī¸ Next Section]