sources/tech/20190830 git exercises- navigate a repository.md
4.8 KiB
git exercises: navigate a repository
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).
https://wizardzines.com/zines/oh-shit-git/samples/ohshit-commit.png
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
exercises
- 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.c
has changed in the last 20ish years: compare that file on the master branch to the file at commit3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4
. - Find a recent commit that changed
hash.c
and 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
v1_8_6_187
and tagv1_8_6_188
- 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
v1_8_6_187
corresponds to. - List the directory
.git/refs/tags
. Runcat .git/refs/tags/v1_8_6_187
to see the contents of one of those files. - Find out what commit ID
HEAD
corresponds to right now. - Find out how many commits have been made to the
test/
directory - Get a diff of
lib/telnet.rb
between the commits65a5162550f58047974793cdc8067a970b2435c0
and9e3d9a2a009d2a0281802a84e1c5cc1c887edc71
. 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_1
andv2_5_3
) (this one is a tiny bit tricky, there’s more than one step) - How many commits were authored by
matz
(Ruby’s creator)? - What’s the most recent commit that included the word
tkutil
? - Check out the commit
e51dca2596db9567bd4d698b18b4d300575d3881
and create a new branch that points at that commit. - Run
git reflog
to see all the navigating of the repository you’ve done so far
via: https://jvns.ca/blog/2019/08/30/git-exercises--navigate-a-repository/
作者:Julia Evans 选题:lujun9972 译者:译者ID 校对:校对者ID