从 https://jvns.ca/blog/2019/08/30/git-exercises–navigate-a-repository/ 看到的关于git的练习。
git clone https://github.com/ruby/ruby ~/github/ruby
本次练习用到的子命令包括
- git checkout
- git log (–oneline, –author, and -S will be useful)
- git diff (–stat will be useful)
- git show
- git status
对git不太熟悉,不能保证一定是正确的,而且还用到了除了git之外的工具. 如果有发现错误或者更好的实现方法,请不吝赐教,谢谢.
- 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.
git checkout 3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4 git branch
- Check out the current master branch
git checkout master git branch
- Look at the history for the file hash.c. What was the last commit ID that changed that file?
git log --oneline -n 1 hash.c
- 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 commit 3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4.
git diff master 3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4 hash.c |head -n 20
- Find a recent commit that changed hash.c and look at the diff for that commit
lastchange=$(git log --oneline -n 2 hash.c |tail -n 1 |cut -f1 -d " ") git diff master ${lastchange} hash.c |head
- This repository has a bunch of tags for every Ruby release. Get a list of all the tags.
git tag |head
- Find out how many files changed between tag v1_8_6_187 and tag v1_8_6_188
git diff v1_8_6_187 v1_8_6_188 --stat
- Find a commit (any commit) from 2015 and check it out, look at the files very briefly, then go back to the master branch.
commit=$(git log --oneline -n 1 --until 2015-01-01 |cut -f1 -d " ") git checkout "${commit}" git branch ls |head git checkout master git branch
- Find out what commit the tag v1_8_6_187 corresponds to.
git rev-list -n 1 v1_8_6_187
- List the directory .git/refs/tags. Run cat .git/refs/tags/v1_8_6_187 to see the contents of one of those files.
ls .git/refs/tags echo ------------------------------------------- cat .git/refs/tags/v1_8_6_187
这个题目好奇怪,.git/refs/tags里居然是空的,感觉有什么地方不对
- Find out what commit ID HEAD corresponds to right now.
git rev-list -n 1 HEAD
- Find out how many commits have been made to the test/ directory
git log --oneline -- test |wc -l
- Find out what commit ID HEAD corresponds to right now.
git rev-list -n 1 HEAD
- Get a diff of lib/telnet.rb between the commits 65a5162550f58047974793cdc8067a970b2435c0 and 9e3d9a2a009d2a0281802a84e1c5cc1c887edc71. How many lines of that file were changed?
git diff --stat 65a5162550f58047974793cdc8067a970b2435c0 9e3d9a2a009d2a0281802a84e1c5cc1c887edc71 -- lib/telnet.rb
- How many commits were made between Ruby 2.5.1 and 2.5.2 (tags v2_5_1 and v2_5_3) (this one is a tiny bit tricky, there’s more than one step)
git log --oneline v2_5_1..v2_5_2 |wc -l
- How many commits were authored by matz (Ruby’s creator)?
git log --oneline --author=matz |wc -l
- What’s the most recent commit that included the word tkutil?
git log -n 1 --grep tkutil
- Check out the commit e51dca2596db9567bd4d698b18b4d300575d3881 and create a new branch that points at that commit.
git branch new e51dca2596db9567bd4d698b18b4d300575d3881 git branch
- Run git reflog to see all the navigating of the repository you’ve done so far
git reflog