I think the curl exercises the other day went well, so today I woke up and wanted to try writing some Git exercises. Git is a big thing to learn, probably too big to learn in a few hours, so my first idea for how to break it down was by starting by navigating a repository.
I was originally going to use a toy test repository, but then I thought – why not a real repository? That’s way more fun! So we’re going to navigate the repository for the Ruby programming language. You don’t need to know any C to do this exercise, it’s just about getting comfortable with looking at how files in a repository change over time.
clone the repository
To get started, clone the repository:
git clone https://github.com/ruby/ruby
The big different thing about this repository (as compared to most of the repositories you’ll work with in real life) is that it doesn’t have branches, but it DOES have lots of tags, which are similar to branches in that they’re both just pointers to a commit. So we’ll do exercises with tags instead of branches. The way you change tags and branches are very different, but the way you look at tags and branches is exactly the same.
a git SHA always refers to the same code
The most important thing to keep in mind while doing these exercises is that a git SHA like
9e3d9a2a009d2a0281802a84e1c5cc1c887edc71 always refers to the same code, as explained in this
page. This page is from a zine I wrote with Katie Sylor-Miller called Oh shit, git!. (She also has a great site called
https://ohshitgit.com/ that inspired the zine).
We’ll be using git SHAs really heavily in the exercises to get you used to working with them and to help understand how they correspond to tags and branches.
git subcommands we’ll be using
All of these exercises only use 5 git subcommands:
git checkout git log (--oneline, --author, and -S will be useful) git diff (--stat will be useful) git show git status
- Check out matz’s commit of Ruby from 1998. The commit ID is
3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4. Find out how many lines of code Ruby was at that time.
- Check out the current master branch
- Look at the history for the file
hash.c. What was the last commit ID that changed that file?
- Get a diff of how
hash.chas changed in the last 20ish years: compare that file on the master branch to the file at commit
- Find a recent commit that changed
hash.cand look at the diff for that commit
- This repository has a bunch of tags for every Ruby release. Get a list of all the tags.
- Find out how many files changed between tag
- Find a commit (any commit) from 2015 and check it out, look at the files very briefly, then go back to the master branch.
- Find out what commit the tag
- List the directory
cat .git/refs/tags/v1_8_6_187to see the contents of one of those files.
- Find out what commit ID
HEADcorresponds to right now.
- Find out how many commits have been made to the
- Get a diff of
lib/telnet.rbbetween the commits
9e3d9a2a009d2a0281802a84e1c5cc1c887edc71. How many lines of that file were changed?
- How many commits were made between Ruby 2.5.1 and 2.5.2 (tags
v2_5_3) (this one is a tiny bit tricky, there’s more than one step)
- How many commits were authored by
- What’s the most recent commit that included the word
- Check out the commit
e51dca2596db9567bd4d698b18b4d300575d3881and create a new branch that points at that commit.
git reflogto see all the navigating of the repository you’ve done so far