Intro to git

Block 2.2: History

Jan Simson

Traveling Through Time: git log

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

Seeing History

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.

Practical: Seeing History

  1. Go back to your Terminal
  2. Open the git log of your respository
  3. Navigate up and down in the history
  4. Copy the commit hash of the 2nd commit you made in the repository
  5. Also explore the git history in a GUI

Properly Traveling Through Time:
git checkout

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

git checkout

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

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

git checkout

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

git checkout 795780d123f6eeedaa09734005c08d1ad89c1976

git checkout

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

git checkout 795780d

Tip

You don’t need to pass the whole commit hash, just the first few characters are usually enough.

git checkout

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

git checkout 795780d
# Note: switching to '795780d123f6eeedaa09734005c08d1ad89c1976'.
#
# 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 795780d Adding hello.txt

Detached HEAD, everything OK? 🤕

  • When traveling back to a commit, we got a scary warning about a detached HEAD
  • Normally we are always on a branch in git
  • The HEAD is always the currently checked out state of your repository
    • If the HEAD is detached, it is not associated with a branch
    • If we make commits with a detached HEAD our changes will be lost
  • We can go back to main with git checkout main

Referencing Commits Relatively

  • You will also see HEAD in e.g. the git log or Sourcetree view
  • HEAD can be used to reference commits relatively
    • HEAD the current commit
    • HEAD~1 the previous commit
    • HEAD~2 two commits ago

Tracking changes: Using a GUI

The labels in Sourcetree mirror the names within the git CLI

Questions?

Practical: Time-Travel ⏱️

  1. Go back to your Terminal
  2. Open the git log of your respository
  3. Checkout the previous commit using HEAD~1
  4. Go back to your main branch
  5. Checkout your very first commit through its hash

End of Section 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Section]