#192 Githug 通关记录及指南

好吧,找到了一个和 git 有关的游戏: https://github.com/Gazler/githug

遂玩之... 挺有意思的..

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
➜  ~/Downloads  sudo gem install githug
Password:
Fetching: mime-types-1.25.1.gem (100%)
Successfully installed mime-types-1.25.1
Fetching: diff-lcs-1.2.5.gem (100%)
Successfully installed diff-lcs-1.2.5
Fetching: grit-2.3.0.gem (100%)
Successfully installed grit-2.3.0
Fetching: thor-0.14.6.gem (100%)
Successfully installed thor-0.14.6
Fetching: githug-0.4.6.gem (100%)
Successfully installed githug-0.4.6
Parsing documentation for mime-types-1.25.1
Installing ri documentation for mime-types-1.25.1
Parsing documentation for diff-lcs-1.2.5
Installing ri documentation for diff-lcs-1.2.5
Parsing documentation for grit-2.3.0
Installing ri documentation for grit-2.3.0
Parsing documentation for thor-0.14.6
Installing ri documentation for thor-0.14.6
Parsing documentation for githug-0.4.6
Installing ri documentation for githug-0.4.6
5 gems installed

第一关 仓库初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  ~/Downloads githug
********************************************************************************
* Githug *
********************************************************************************
No githug directory found, do you wish to create one? [yn] y
Welcome to Githug!

Name: init
Level: 1
Difficulty: *

A new directory, `git_hug`, has been created; initialize an empty repository in it.
➜ ~/Downloads/git_hug githug hint
********************************************************************************
* Githug *
********************************************************************************
You can type `git` in your shell to get a list of available git commands.
➜ ~/Downloads/githug/git_hug git init
Initialized empty Git repository in /Users/ouchangkun/Downloads/githug/git_hug/.git/

第二关 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: config
Level: 2
Difficulty: *

Set up your git name and email, this is important so that your commits can be identified.

➜ git_hug master ✓ git config user.name changkun
➜ git_hug master ✓ git config user.email hi[_at_]changkun.de

第三关 Stage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
What is your name? changkun
What is your email? hi[_at_]changkun.de
Your config has the following name: changkun
Your config has the following email: hi[_at_]changkun.de
Congratulations, you have solved the level!

Name: add
Level: 3
Difficulty: *

There is a file in your folder called `README`, you should add it to your staging area
Note: You start each level with a new repo. Don't look for files from the previous one.

➜ git_hug master ✗ git add README

第四关 commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: commit
Level: 4
Difficulty: *

The `README` file has been added to your staging area, now commit it.
➜ git_hug master ✗ git commit -m "add READEME"
[master (root-commit) ea6e169] add READEME
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README

第五关 clone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: clone
Level: 5
Difficulty: *

Clone the repository at https://github.com/Gazler/cloneme.


➜ ~/Downloads/githug/git_hug git clone https://github.com/Gazler/cloneme
Cloning into 'cloneme'...
remote: Counting objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (7/7), done.
Checking connectivity... done.

第六关 clone 到文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  ~/Downloads/githug/git_hug  githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: clone_to_folder
Level: 6
Difficulty: *

Clone the repository at https://github.com/Gazler/cloneme to `my_cloned_repo`.

➜ ~/Downloads/githug/git_hug git clone https://github.com/Gazler/cloneme my_cloned_repo
Cloning into 'my_cloned_repo'...
remote: Counting objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (7/7), done.
Checking connectivity... done.

第七关 .gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  ~/Downloads/githug/git_hug  githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: ignore
Level: 7
Difficulty: **

The text editor 'vim' creates files ending in `.swp` (swap files) for all files that are currently open. We don't want them creeping into the repository. Make this repository ignore `.swp` files.

➜ git_hug master ✗ vi .gitignore
add *.swp

第八关 .gitignore 规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: include
Level: 8
Difficulty: **

Notice a few files with the '.a' extension. We want git to ignore all but the 'lib.a' file.
➜ git_hug master ✗ vi .gitignore
*.a
!lib.a

第九关 status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
➜  git_hug master ✗ githug       
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: status
Level: 9
Difficulty: *

There are some files in this repository, one of the files is untracked, which file is it?

➜ git_hug master ✗ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: Guardfile
new file: README
new file: config.rb
new file: deploy.rb
new file: setup.rb

Untracked files:
(use "git add <file>..." to include in what will be committed)

database.yml

第十关 commit 文件数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
What is the full file name of the untracked file? database.yml
Congratulations, you have solved the level!

Name: number_of_files_committed
Level: 10
Difficulty: *

There are some files in this repository, how many of the files will be committed?
➜ git_hug master ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: rubyfile1.rb
modified: rubyfile4.rb

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: rubyfile5.rb

Untracked files:
(use "git add <file>..." to include in what will be committed)

rubyfile6.rb
rubyfile7.rb

第十一关 移除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
How many changes are going to be committed? 2
Congratulations, you have solved the level!

Name: rm
Level: 11
Difficulty: **

A file has been removed from the working tree, however the file was not removed from the repository. Find out what this file was and remove it.
➜ git_hug master ✗ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: deleteme.rb

no changes added to commit (use "git add" and/or "git commit -a")
➜ git_hug master ✗ git rm deleteme.rb
rm 'deleteme.rb'

第十二关 取消追踪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: rm_cached
Level: 12
Difficulty: **

A file has accidentally been added to your staging area, find out which file and remove it from the staging area. *NOTE* Do not remove the file from the file system, only from git.

➜ git_hug master ✗ git rm --cached deleteme.rb
rm 'deleteme.rb'

第十三关 hash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: stash
Level: 13
Difficulty: **

You've made some changes and want to work on them later. You should save them, but don't commit them.

➜ git_hug master ✗ git stash
Saved working directory and index state WIP on master: 0206059 Add some lyrics
HEAD is now at 0206059 Add some lyrics

第十四关 重命名

1
2
3
4
5
6
7
8
9
10
11
12
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: rename
Level: 14
Difficulty: ***

We have a file called `oldfile.txt`. We want to rename it to `newfile.txt` and stage this change.
➜ git_hug master ✓ git mv oldfile.txt newfile.txt

第十五关 重构

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: restructure
Level: 15
Difficulty: ***

You added some files to your repository, but now realize that your project needs to be restructured. Make a new folder named `src` and using Git move all of the .html files into this folder.
➜ git_hug master ✓ mkdir src
➜ git_hug master ✓ git mv *.html src/

第十六关 日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: log
Level: 16
Difficulty: **

You will be asked for the hash of most recent commit. You will need to investigate the logs of the repository for this.

➜ git_hug master ✓ git log
check the first 7 characters

第十七关 tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
What is the hash of the most recent commit? d4dcdfe
Congratulations, you have solved the level!

Name: tag
Level: 17
Difficulty: **

We have a git repo and we want to tag the current commit with `new_tag`.

➜ git_hug master ✓ git tag 'new_tag'

第十八关 提交 tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: push_tags
Level: 18
Difficulty: **

There are tags in the repository that aren't pushed into remote repository. Push them now.


From /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160119-70224-1azgvro/
* [new branch] master -> origin/master

➜ git_hug master ✓ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
To /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160119-70224-1azgvro/.git
* [new tag] tag_to_be_pushed -> tag_to_be_pushed

第十九关 修正 commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: commit_amend
Level: 19
Difficulty: **

The `README` file has been committed, but it looks like the file `forgotten_file.rb` was missing from the commit. Add the file and amend your previous commit to include it.

➜ git_hug master ✗ git add forgotten_file.rb
➜ git_hug master ✗ git commit --amend
[master 45606ca] Initial commit
Date: Tue Jan 19 14:50:12 2016 +0100
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 forgotten_file.rb

第二十关 设置 commit 时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: commit_in_future
Level: 20
Difficulty: **

Commit your changes with the future date (e.g. tomorrow).

➜ git_hug master ✗ githug hint
********************************************************************************
* Githug *
********************************************************************************
Build a time machine, move to the future and commit your changes, then go back and verify results ;).

这个提示是个什么鬼啊(╯‵□′)╯︵┻━┻要不要这么卖萌

1
2
3
4
5
➜  git_hug master ✗ git commit --date=2016-01-20
[master (root-commit) 349dbee] Yo..
Date: Wed Jan 20 14:56:10 2016 +0100
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
## 第二十一关 重置
1
2
3
4
5
6
7
8
9
10
11
12
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: reset
Level: 21
Difficulty: **

There are two files to be committed. The goal was to add each file as a separate commit, however both were added by accident. Unstage the file `to_commit_second.rb` using the reset command (don't commit anything).
➜ git_hug master ✗ git reset HEAD to_commit_second.rb
## 第二十二关 撤销 commit
1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: reset_soft
Level: 22
Difficulty: **

You committed too soon. Now you want to undo the last commit, while keeping the index.

➜ git_hug master ✓ git reset --soft HEAD^

第二十三关 撤销 commited 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: checkout_file
Level: 23
Difficulty: ***

A file has been modified, but you don't want to keep the modification. Checkout the `config.rb` file from the last commit.

➜ git_hug master ✗ git checkout -- config.rb

第二十四关 远程仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: remote
Level: 24
Difficulty: **

This project has a remote repository. Identify it.
➜ git_hug master ✓ git remote
my_remote_repo

第二十五关 远程仓库地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
What is the name of the remote repository? my_remote_repo
Congratulations, you have solved the level!

Name: remote_url
Level: 25
Difficulty: **

The remote repositories have a url associated to them. Please enter the url of remote_location.

➜ git_hug master ✓ git remote -v
my_remote_repo https://github.com/Gazler/githug (fetch)
my_remote_repo https://github.com/Gazler/githug (push)
remote_location https://github.com/githug/not_a_repo (fetch)
remote_location https://github.com/githug/not_a_repo (push)

第二十六关 pull

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  git_hug master ✓ githug 
********************************************************************************
* Githug *
********************************************************************************
What is the url of the remote repository? https://github.com/githug/not_a_repo
Congratulations, you have solved the level!

Name: pull
Level: 26
Difficulty: **

You need to pull changes from your origin repository.

➜ git_hug master ✓ git pull origin master
From https://github.com/pull-this/thing-to-pull
* branch master -> FETCH_HEAD

第二十七关 关联远程库

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: remote_add
Level: 27
Difficulty: **

Add a remote repository called `origin` with the url https://github.com/githug/githug

➜ git_hug master ✓ git remote add origin https://github.com/githug/githug

第二十八关 rebase & push

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: push
Level: 28
Difficulty: ***

Your local master branch has diverged from the remote origin/master branch. Rebase your commit onto origin/master and push it to remote.


remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160218-70107-mxai23/
* [new branch] master -> origin/master


➜ git_hug master ✓ git rebase
First, rewinding head to replay your work on top of it...
Applying: Third commit
➜ git_hug master ✓ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 228 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160218-70107-mxai23/.git
6e57a37..bcdf76f master -> master

第二十九关 diff

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: diff
Level: 29
Difficulty: **

There have been modifications to the `app.rb` file since your last commit. Find out which line has changed.

➜ git_hug master ✗ git diff app.rb

第三十关 查看修改日志

1
2
3
4
5
6
7
8
9
10
11
12
13
********************************************************************************
* Githug *
********************************************************************************
What is the number of the line which has changed? 26
Congratulations, you have solved the level!

Name: blame
Level: 30
Difficulty: **

Someone has put a password inside the file `config.rb` find out who it was.

➜ git_hug master ✗ git log -p

或者

1
➜  git_hug master ✗ git blame config.rb
## 第三十一关 branch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Who made the commit with the password? Spider Man
Congratulations, you have solved the level!

Name: branch
Level: 31
Difficulty: *

You want to work on a piece of code that has the potential to break things, create the branch test_code.

➜ git_hug master ✓ git branch test_code

第三十二关 checkout

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: checkout
Level: 32
Difficulty: **

Create and switch to a new branch called my_branch. You will need to create a branch like you did in the previous level.

➜ git_hug master ✓ git checkout -b my_branch

第三十三关 checkout tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
➜  git_hug my_branch ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: checkout_tag
Level: 33
Difficulty: **

You need to fix a bug in the version 1.2 of your app. Checkout the tag `v1.2`.
➜ git_hug master ✓ githug hint
********************************************************************************
* Githug *
********************************************************************************
There's no big difference between checking out a branch and checking out a tag.
➜ git_hug master ✓ git checkout v1.2
Note: checking out 'v1.2'.

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 performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 618b73b... Some more changes

第三十四关 区分 tag 和 branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
➜  git_hug 618b73b ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: checkout_tag_over_branch
Level: 34
Difficulty: **

You need to fix a bug in the version 1.2 of your app. Checkout the tag `v1.2` (Note: There is also a branch named `v1.2`).

➜ git_hug master ✓ git checkout tags/v1.2
Note: checking out 'tags/v1.2'.

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 performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 8a21ee6... Some more changes

第三十五关 在历史 commit 中创建分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug 8a21ee6 ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: branch_at
Level: 35
Difficulty: ***

You forgot to branch at the previous commit and made a commit on top of it. Create branch test_branch at the commit before the last.

➜ git_hug master ✓ git log
➜ git_hug master ✓ git branch test_branch 8a8f0949f431f727d88a062f4470c1fded9e23d3

第三十六关 删除分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: delete_branch
Level: 36
Difficulty: **

You have created too many branches for your project. There is an old branch in your repo called 'delete_me', you should delete it.

➜ git_hug master ✓ git branch -d delete_me
Deleted branch delete_me (was b60afe2).

第三十七关 push 分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
➜  git_hug master ✓ githug 
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: push_branch
Level: 37
Difficulty: **

You've made some changes to a local branch and want to share it, but aren't yet ready to merge it with the 'master' branch. Push only 'test_branch' to the remote repository

➜ git_hug master ✓ git push origin test_branch
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 578 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
To /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160219-82338-1ge2wsz/.git
* [new branch] test_branch -> test_branch

第三十八关 merge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: merge
Level: 38
Difficulty: **

We have a file in the branch 'feature'; Let's merge it to the master branch.


➜ git_hug master ✓ git merge master feature
Updating e12277f..cc8ea5a
Fast-forward
file2 | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2

第三十九关 fetch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: fetch
Level: 39
Difficulty: **

Looks like a new branch was pushed into our remote repository. Get the changes without merging them with the local repository

➜ git_hug master ✓ git fetch
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From /var/folders/st/cs12yhw15xn0yk4mncx6j7m00000gn/T/d20160219-84409-c6yi4z/
* [new branch] new_branch -> origin/new_branch

第四十关 rebase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: rebase
Level: 40
Difficulty: **

We are using a git rebase workflow and the feature branch is ready to go into master. Let's rebase the feature branch onto our master branch.

➜ git_hug master ✓ git checkout feature
Switched to branch 'feature'
➜ git_hug feature ✓ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add feature

第四十一关 repack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  git_hug feature ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: repack
Level: 41
Difficulty: **

Optimise how your repository is packaged ensuring that redundant packs are removed.

➜ git_hug master ✓ git gc
Counting objects: 3, done.
Writing objects: 100% (3/3), done.
Total 3 (delta 0), reused 0 (delta 0)

第四十二关 cherry-pick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: cherry-pick
Level: 42
Difficulty: ***

Your new feature isn't worth the time and you're going to delete it. But it has one commit that fills in `README` file, and you want this commit to be on the master as well.

➜ git_hug master ✓ git branch
* master
new-feature
➜ git_hug master ✓ git checkout new-feature
Switched to branch 'new-feature'
➜ git_hug new-feature ✓ git log
➜ git_hug new-feature ✓ git checkout master
Switched to branch 'master'
➜ git_hug master ✓ git cherry-pick ca32a6dac7b6f97975edbe19a4296c2ee7682f68
[master 1eba631] Filled in README.md with proper input
Author: Andrey <aslushnikov@gmail.com>
Date: Wed Mar 28 02:25:51 2012 +0400
1 file changed, 1 insertion(+), 2 deletions(-)

第四十三关 git-grep

1
2
3
4
5
6
7
8
9
10
11
12
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: grep
Level: 43
Difficulty: **

Your project's deadline approaches, you should evaluate how many TODOs are left in your code
➜ git_hug master ✓ git grep 'TODO' | wc -l
4

第四十四关 修改历史日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
How many items are there in your todolist? 4
Congratulations, you have solved the level!

Name: rename_commit
Level: 44
Difficulty: ***

Correct the typo in the message of your first (non-root) commit.

➜ git_hug master ✓ git rebase -i HEAD~2
[detached HEAD bbabd3f] First commit
Date: Sat Feb 20 08:02:54 2016 +0100
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Successfully rebased and updated refs/heads/master.

这里要求修改 commit 内容,里面的 commit 被写成了 coommit。

第四十五关 合并多次 commit

1
2
3
4
5
6
7
8
9
10
11
12
➜  git_hug master ✓ githug 
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: squash
Level: 45
Difficulty: ****

You have committed several times but would like all those changes to be one commit.
➜ git_hug master ✓ git log

这里可以看到最近三次 commit 都要求被合并到 倒数第四次 commit 中。

1
➜  git_hug master ✓ git rebase -i HEAD~4
看下面的要求,把第二行至第四行的 pick 改为 s 或 squash 即可。

第四十六关 合并提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
➜  git_hug master ✓ githug              
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: merge_squash
Level: 46
Difficulty: ***

Merge all commits from the long-feature-branch as a single commit.

➜ git_hug master ✓ git merge --squash long-feature-branch
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
➜ git_hug master ✗ git log
➜ git_hug master ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: file3

➜ git_hug master ✗ git commit -a -m "merge all from long feature branch"
[master a8ca042] merge all from long feature branch
1 file changed, 3 insertions(+)
create mode 100644 file3

第四十七关 调整 commit 顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: reorder
Level: 47
Difficulty: ****

You have committed several times but in the wrong order. Please reorder your commits.

➜ git_hug master ✓ git log

能够看到倒数两次的 commit 顺序错误。

1
2
➜  git_hug master ✓ git rebase -i HEAD~2
Successfully rebased and updated refs/heads/master.

将 pick 两行顺序对调,1yy 可以复制整行,p 可以进行粘贴(此属于 vim 的内容)。

第四十八关 测试驱动排错

1
2
3
4
5
6
7
8
9
10
11
12
➜  git_hug master ✓ githug              
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: bisect
Level: 48
Difficulty: ***

A bug was introduced somewhere along the way. You know that running `ruby prog.rb 5` should output 15. You can also run `make test`. What are the first 7 chars of the hash of the commit that introduced the bug.
➜ git_hug master ✓ git log --reverse -p prog.rb

我们从第一次提交来开始搜索,这里能够拿到第一次 commit 的 hash 为 f608824888b83bbedc1f658be7496ffea467a8fb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
➜  git_hug master ✓ git bisect start
➜ git_hug master ✓ git bisect good f608824888b83bbedc1f658be7496ffea467a8fb
➜ git_hug master ✓ git bisect bad
Bisecting: 9 revisions left to test after this (roughly 3 steps)
[fdbfc0d403e5ac0b2659cbfa2cbb061fcca0dc2a] Another Commit
➜ git_hug fdbfc0d ✓ git bisect run make test
running make test
ruby prog.rb 5 | ruby test.rb
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[18ed2ac1522a014412d4303ce7c8db39becab076] Another Commit
running make test
ruby prog.rb 5 | ruby test.rb
make: *** [test] Error 1
Bisecting: 2 revisions left to test after this (roughly 1 step)
[9f54462abbb991b167532929b34118113aa6c52e] Another Commit
running make test
ruby prog.rb 5 | ruby test.rb
Bisecting: 0 revisions left to test after this (roughly 1 step)
[5db7a7cb90e745e2c9dbdd84810ccc7d91d92e72] Another Commit
running make test
ruby prog.rb 5 | ruby test.rb
18ed2ac1522a014412d4303ce7c8db39becab076 is the first bad commit
commit 18ed2ac1522a014412d4303ce7c8db39becab076
Author: Robert Bittle <guywithnose@gmail.com>
Date: Mon Apr 23 06:52:10 2012 -0400

Another Commit

:100644 100644 917e70054c8f4a4a79a8e805c0e1601b455ad236 7562257b8e6446686ffc43a2386c50c254365020 Mprog.rb
bisect run success

上面能够看到 18ed2ac1522a014412d4303ce7c8db39becab076 is the first bad commit。

第四十九关 stage 单个文件的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
➜  git_hug 5db7a7c ✓ githug
********************************************************************************
* Githug *
********************************************************************************
What are the first 7 characters of the hash of the commit that introduced the bug? 18ed2ac1522a014412d4303ce7c8db39becab076
Congratulations, you have solved the level!

Name: stage_lines
Level: 49
Difficulty: ****

You've made changes within a single file that belong to two different features, but neither of the changes are yet staged. Stage only the changes belonging to the first feature.

➜ git_hug master ✗ l
total 24
drwxr-xr-x 6 ouchangkun staff 204B Feb 20 08:36 .
drwxr-xr-x 3 ouchangkun staff 102B Jan 12 18:44 ..
drwxr-xr-x 13 ouchangkun staff 442B Feb 20 08:37 .git
-rw-r--r-- 1 ouchangkun staff 24B Feb 20 08:36 .gitignore
-rw-r--r-- 1 ouchangkun staff 1.2K Feb 20 08:36 .profile.yml
-rw-r--r-- 1 ouchangkun staff 115B Feb 20 08:36 feature.rb
➜ git_hug master ✗ vim feature.rb
➜ git_hug master ✗ githug hint
********************************************************************************
* Githug *
********************************************************************************
You might want to try to manipulate the hunks of the diff to choose which lines of the diff get staged. Read about the flags which can be passed to the `add` command; `man git-add`.
➜ git_hug master ✗ git add -p feature.rb
diff --git a/feature.rb b/feature.rb
index 1a271e9..4a80dda 100644
--- a/feature.rb
+++ b/feature.rb
@@ -1 +1,3 @@
this is the class of my feature
+This change belongs to the first feature
+This change belongs to the second feature
Stage this hunk [y,n,q,a,d,/,e,?]? e

进入编辑后删除 The second feature 所在行(dd)。

第五十关 查看操作历史

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
➜  git_hug master ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: find_old_branch
Level: 50
Difficulty: ****

You have been working on a branch but got distracted by a major issue and forgot the name of it. Switch back to that branch.

➜ git_hug kill_the_batman ✓ git branch
blowup_sun_for_ransom
cure_common_cold
* kill_the_batman
solve_world_hunger
➜ git_hug kill_the_batman ✓ githug hint
********************************************************************************
* Githug *
********************************************************************************
Ever played with the `git reflog` command?
➜ git_hug kill_the_batman ✓ git reflog
894a16d HEAD@{0}: commit: commit another todo
6876e5b HEAD@{1}: checkout: moving from solve_world_hunger to kill_the_batman
324336a HEAD@{2}: commit: commit todo
6876e5b HEAD@{3}: checkout: moving from blowup_sun_for_ransom to solve_world_hunger
6876e5b HEAD@{4}: checkout: moving from kill_the_batman to blowup_sun_for_ransom
6876e5b HEAD@{5}: checkout: moving from cure_common_cold to kill_the_batman
6876e5b HEAD@{6}: commit (initial): initial commit

➜ git_hug kill_the_batman ✓ git checkout solve_world_hunger
Switched to branch 'solve_world_hunger'

第五十一关 撤销某次历史提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  git_hug solve_world_hunger ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: revert
Level: 51
Difficulty: ****

You have committed several times but want to undo the middle commit.
All commits have been pushed, so you can't change existing history.

➜ git_hug master ✓ git log
➜ git_hug master ✓ git revert 35fc609fd0541bdec3386e3264c0b2629586cf4c
[master b0611f2] Revert "Bad commit"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file3

第五十二关 返回历史版本 or 从历史版本中恢复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: restore
Level: 52
Difficulty: ****

You decided to delete your latest commit by running `git reset --hard HEAD^`. (Not a smart thing to do.) You then change your mind, and want that commit back. Restore the deleted commit.

➜ git_hug master ✓ git reset --help
➜ git_hug master ✓ git reflog
db161d7 HEAD@{0}: reset: moving to HEAD^
b991b7c HEAD@{1}: commit: Restore this commit
db161d7 HEAD@{2}: commit: First commit
ecbf088 HEAD@{3}: commit (initial): Initial commit
➜ git_hug master ✓ git checkout b991b7c
Note: checking out 'b991b7c'.

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 performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at b991b7c... Restore this commit

虽然我直接切到历史版本中也过了。。但是这题的本意是要求从历史版本中恢复,所以其实应该使用:

1
✓ git cherry-pick b991b7c

第五十三关 解决冲突

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➜  git_hug b991b7c ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: conflict
Level: 53
Difficulty: ****

You need to merge mybranch into the current branch (master). But there may be some incorrect changes in mybranch which may cause conflicts. Solve any merge-conflicts you come across and finish the merge.

➜ git_hug master ✓ git merge mybranch
Auto-merging poem.txt
CONFLICT (content): Merge conflict in poem.txt
Automatic merge failed; fix conflicts and then commit the result.
➜ git_hug master ✗ vim poem.txt

删除文本中的冲突标记,再 commit。

1
2
3
➜  git_hug master ✗ git add poem.txt 
➜ git_hug master ✗ git commit -m "merge conflict solving"
[master 1e85297] merge conflict solving

第五十四关 使用子模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  git_hug master ✓ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

Name: submodule
Level: 54
Difficulty: **

You want to include the files from the following repo: `https://github.com/jackmaney/githug-include-me` into a the folder `./githug-include-me`. Do this without cloning the repo or copying the files from the repo into this repo.

➜ git_hug master ✓ git submodule add https://github.com/jackmaney/githug-include-me ./githug-include-me
Cloning into 'githug-include-me'...
remote: Counting objects: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 9
Unpacking objects: 100% (9/9), done.
Checking connectivity... done.

第五十五关 是什么呢

自己玩吧:)

打赏催更