githug小游戏通关笔记

这货叫做Githug,主要目的是通过游戏的形式来练习git命令的使用。

1
2
3
4
5
6
➜  ruby githug
********************************************************************************
* Githug *
********************************************************************************
No githug directory found, do you wish to create one? [yn] y
Welcome to Githug!

1 - 5

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Name: init
Level: 1
Difficulty: *

A new directory, `git_hug`, has been created; initialize an empty repository in it.


➜ ruby git config --global core.editor "/usr/bin/vim"
➜ ruby cd git_hug
➜ git_hug git init
Initialized empty Git repository in /Users/yangxiao/Documents/ruby/git_hug/.git/
➜ git_hug git:(master) ls
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: config
Level: 2
Difficulty: *

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


➜ git_hug git:(master) git config user.name sagittariusyx
➜ git_hug git:(master) git config user.email "ssdut.xiao@gmail.com"
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
What is your name? sagittariusyx
What is your email? ssdut.xiao@gmail.com
Your config has the following name: sagittariusyx
Your config has the following email: ssdut.xiao@gmail.com
Congratulations, you have solved the level!

3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 git:(master) ✗ ls
README
➜ git_hug git:(master) ✗ vim README
➜ git_hug git:(master) ✗ git add README
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name: commit
Level: 4
Difficulty: *

The `README` file has been added to your staging area, now commit it.


➜ git_hug git:(master) ✗ git commit -m "init"
[master (root-commit) 17922c4] init
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: clone
Level: 5
Difficulty: *

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


➜ 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.
➜ git_hug githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

6 - 10

6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: clone_to_folder
Level: 6
Difficulty: *

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


➜ 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.
➜ git_hug githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 those swap files which are ending in `.swp`.


➜ git_hug git:(master) ✗ vim .gitignore
*.swp
[exit VIM]
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 git:(master) ✗ vim .gitignore
*.a
!lib.a
[exit VIM]
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

9

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
Name: status
Level: 9
Difficulty: *

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


➜ git_hug git:(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

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
What is the full file name of the untracked file? database.yml
Congratulations, you have solved the level!

10

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
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 git:(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

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
How many changes are going to be committed? 2
Congratulations, you have solved the level!

11 - 15

11

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
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 git:(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 git:(master) ✗ git rm deleteme.rb
rm 'deleteme.rb'
➜ git_hug git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted: deleteme.rb

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

12

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
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 git:(master) ✗ git status
On branch master

Initial commit

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

new file: deleteme.rb

➜ git_hug git:(master) ✗ git rm --cached deleteme.rb
rm 'deleteme.rb'
➜ git_hug git:(master) ✗ git status
On branch master

Initial commit

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

deleteme.rb

nothing added to commit but untracked files present (use "git add" to track)
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 git:(master) ✗ git status
On branch master
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: lyrics.txt

no changes added to commit (use "git add" and/or "git commit -a")
➜ git_hug git:(master) ✗ git stash
Saved working directory and index state WIP on master: 0206059 Add some lyrics
HEAD is now at 0206059 Add some lyrics
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 git:(master) git mv oldfile.txt newfile.txt
➜ git_hug git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed: oldfile.txt -> newfile.txt

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

15

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
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 git:(master) ls
about.html contact.html index.html
➜ git_hug git:(master) mkdir src
➜ git_hug git:(master) git mv *.html src
➜ git_hug git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed: about.html -> src/about.html
renamed: contact.html -> src/contact.html
renamed: index.html -> src/index.html

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

16 - 20

16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 git:(master) git log
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
What is the hash of the most recent commit? 053adb5
Congratulations, you have solved the level!

17

1
2
3
4
5
6
7
8
9
10
11
12
13
Name: tag
Level: 17
Difficulty: **

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


➜ git_hug git:(master) git tag new_tag
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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/6v/0_m2zjlx1_vbvv4wgmlq0qm40000gn/T/d20170117-10523-xtjnrf/
* [new branch] master -> origin/master
➜ git_hug git:(master) git status
On branch master
nothing to commit, working directory clean
➜ git_hug git:(master) githug --tags git push ?????
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

19

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
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 git:(master) ✗ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

forgotten_file.rb

nothing added to commit but untracked files present (use "git add" to track)
➜ git_hug git:(master) ✗ git add forgotten_file.rb
➜ git_hug git:(master) ✗ git commit --amend

*****[commit with VIM]*****

[master 423cadb] Initial commit
Date: Tue Jan 17 20:37:55 2017 +0800
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 forgotten_file.rb
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name: commit_in_future
Level: 20
Difficulty: **

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

➜ git_hug git:(master) ✗ git commit --date="Tue Jan 18 21:12:01 CST 2017"
[master (root-commit) d364e1a] commit future!
Date: Wed Jan 18 21:12:01 2017 -0600
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

21 - 25

21

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: to_commit_first.rb
new file: to_commit_second.rb

➜ git_hug git:(master) ✗ git reset HEAD to_commit_second.rb
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Name: reset_soft
Level: 22
Difficulty: **

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


➜ git_hug git:(master) git reset --soft HEAD^1
➜ git_hug git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: newfile.rb

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

23

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 git:(master) ✗ git status
On branch master
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: config.rb

no changes added to commit (use "git add" and/or "git commit -a")
➜ git_hug git:(master) ✗ git checkout config.rb
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

24

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Name: remote
Level: 24
Difficulty: **

This project has a remote repository. Identify it.

➜ git_hug git:(master) git remote
my_remote_repo
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
What is the name of the remote repository? my_remote_repo
Congratulations, you have solved the level!

25

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: remote_url
Level: 25
Difficulty: **

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


➜ git_hug git:(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)
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
What is the url of the remote repository? https://github.com/githug/not_a_repo
Congratulations, you have solved the level!

26 - 30

26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Name: pull
Level: 26
Difficulty: **

You need to pull changes from your origin repository.


➜ git_hug git:(master) git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.
From https://github.com/pull-this/thing-to-pull
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

27

1
2
3
4
5
6
7
8
9
10
11
12
13
Name: remote_add
Level: 27
Difficulty: **

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


➜ git_hug git:(master) git remote add origin https://github.com/githug/githug
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

28

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.

➜ git_hug git:(master) git rebase origin/master
First, rewinding head to replay your work on top of it...
Applying: First commit
Applying: Second commit
Applying: Third commit
➜ git_hug git:(master) git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 612 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To /var/folders/6v/0_m2zjlx1_vbvv4wgmlq0qm40000gn/T/d20170120-2068-u1sjza/.git
f3aeb43..2e33184 master -> master
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

29

1
2
3
4
5
6
7
8
9
10
11
12
13
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 git:(master) ✗ git diff app.rb
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
What is the number of the line which has changed? 26
Congratulations, you have solved the level!

30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Name: blame
Level: 30
Difficulty: **

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


➜ git_hug git:(master) ✗ git blame config.rb
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Who made the commit with the password? Spider Man
Congratulations, you have solved the level!

31 - 35

31

1
2
3
4
5
6
7
8
9
10
11
12
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 git:(master) git branch test_code
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

32

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 git:(master) git checkout -b my_branch
Switched to a new branch 'my_branch'
➜ git_hug git:(my_branch) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

33

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 git:(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 f83bf75... Some more changes
➜ git_hug git:(f83bf75) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

34

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
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 git:(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 2aede5e... Some more changes
➜ git_hug git:(2aede5e) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

35

1
2
3
4
5
6
7
8
9
10
11
12
13
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 git:(master) git branch test_branch HEAD^1
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

36 - 40

36

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 git:(master) git branch -d delete_me
Deleted branch delete_me (was b60afe2).
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

37

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 git:(master) git push origin test_branch
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 585 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
To /var/folders/6v/0_m2zjlx1_vbvv4wgmlq0qm40000gn/T/d20170120-2746-zswfij/.git
* [new branch] test_branch -> test_branch
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

38

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: merge
Level: 38
Difficulty: **

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


➜ git_hug git:(master) git merge feature
Updating e12277f..cc8ea5a
Fast-forward
file2 | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

39

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 git:(master) git fetch origin
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/6v/0_m2zjlx1_vbvv4wgmlq0qm40000gn/T/d20170120-2817-3ndtsr/
* [new branch] new_branch -> origin/new_branch
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

40

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 git:(master) git checkout feature
Switched to branch 'feature'
➜ git_hug git:(feature) git rebase master
First, rewinding head to replay your work on top of it...
Applying: add feature
➜ git_hug git:(feature) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

41 - 45

41

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Name: rebase_onto
Level: 41
Difficulty: **

You have created your branch from `wrong_branch` and already made some commits, and you realise that you needed to create your branch from `master`. Rebase your commits onto `master` branch so that you don't have `wrong_branch` commits.


➜ git_hug git:(readme-update) git rebase --onto master wrong_branch
First, rewinding head to replay your work on top of it...
Applying: Add app name in readme
Applying: Add `About` header in readme
Applying: Add `Install` header in readme
➜ git_hug git:(readme-update) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

42

git gc 需要深研究搞明白,稍后我会整理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name: repack
Level: 42
Difficulty: **

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


➜ git_hug git:(master) git gc
Counting objects: 3, done.
Writing objects: 100% (3/3), done.
Total 3 (delta 0), reused 0 (delta 0)
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

43

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Name: cherry-pick
Level: 43
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 git:(master) git branch
* master
new-feature
➜ git_hug git:(master) git checkout new-feature
Switched to branch 'new-feature'
➜ git_hug git:(new-feature) git log
➜ git_hug git:(new-feature) git checkout master
Switched to branch 'master'
➜ git_hug git:(master) git cherry-pick ca32a6dac7b6f97975edbe19a4296c2ee7682f68
[master be39958] 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_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

44

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Name: grep
Level: 44
Difficulty: **

Your project's deadline approaches, you should evaluate how many TODOs are left in your code


➜ git_hug git:(master) git grep 'TODO' | wc -l
4
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
How many items are there in your todolist? 4
Congratulations, you have solved the level!

45

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Name: rename_commit
Level: 45
Difficulty: ***

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

➜ git_hug git:(master) git rebase -i HEAD~2

# 修改前面pick为reword

[detached HEAD 35c7147] First commit
Date: Fri Jan 20 22:33:15 2017 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Successfully rebased and updated refs/heads/master.
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

46 - 50

46

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Name: squash
Level: 46
Difficulty: ****

You have committed several times but would like all those changes to be one commit.

➜ git_hug git:(master) git log
➜ git_hug git:(master) git rebase -i HEAD~4

[把后三个命令都改为s]

[detached HEAD b21b641] Adding README
Date: Wed Jan 25 20:33:56 2017 +0800
1 file changed, 3 insertions(+)
create mode 100644 README
Successfully rebased and updated refs/heads/master.
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

47

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
Name: merge_squash
Level: 47
Difficulty: ***

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


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

new file: file3

➜ git_hug git:(master) ✗ git commit -m "XXX"
[master e29f6b4] XXX
1 file changed, 3 insertions(+)
create mode 100644 file3
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

48

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Name: reorder
Level: 48
Difficulty: ****

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


➜ git_hug git:(master) git log
➜ git_hug git:(master) git rebase -i HEAD~3

[颠倒一下顺序]

Successfully rebased and updated refs/heads/master.
➜ git_hug git:(master) git log
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

49

1
2
3
4
5
Name: bisect
Level: 49
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.

项目中有个 bug 是某一次 commit 引入的。使用 make test 可以测试项目,返回值是 0 的话表示测试通过,无 bug。要求找出引入 bug 的那个 commit。

git birect 的基本用法如下:

  1. 执行 git birect start,表示开始二分搜索;
  2. 找到任意一个有问题的 commit,标记一下这个版本。一般这种情况下当前版本都是有问题的,所以 git birect bad
  3. 找到任意一个没问题的 commit,标记一下这个版本。如 git birect good f608824888b83bbedc1f658be7496ffea467a8fb
  4. 当你至少标记了一个 good 和一个 bad 之后,就会看到类似于 Bisecting: 675 revisions left to test after this 的提示,告诉你你标记的两个版本之间有这么多个待测试的版本。
  5. 然后两者中间的那个版本会自动被 checkout,你要做的是测试一下这个版本有没有问题,并标记为 git birect good 或 bad
  6. git 会自动重复第 4 步,然后你重复第 5 步。这样一直二分搜索下来,直到找到第一个有问题的版本。

当然,这样一个一个版本手工测试还是很麻烦,如果你的项目有没有 bug 可以通过脚本自动检测,然后通过返回 0 来表示测试通过,返回其他值表示不通过,那么就可以省下手工测试的环节了。直接在第 4 步标记完之后,执行 git run <你的测试脚本>,git 将自动重复执行 第 4、5 步,直至找到第一个有问题的版本。

整体solution如下:

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
54
55
➜  git_hug git:(master) git bisect start
➜ git_hug git:(master) make test
ruby prog.rb 5 | ruby test.rb
make: *** [test] Error 1
➜ git_hug git:(master) git bisect bad
➜ git_hug git:(master) git log
➜ git_hug git:(master) git checkout f608824888b83bbedc1f658be7496ffea467a8fb
Note: checking out 'f608824888b83bbedc1f658be7496ffea467a8fb'.

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 f608824... First commit
➜ git_hug git:(f608824) make test
ruby prog.rb 5 | ruby test.rb
➜ git_hug git:(f608824) git bisect good
Bisecting: 9 revisions left to test after this (roughly 3 steps)
[fdbfc0d403e5ac0b2659cbfa2cbb061fcca0dc2a] Another Commit
➜ git_hug git:(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 M prog.rb
bisect run success
➜ git_hug git:(5db7a7c) githug
********************************************************************************
* Githug *
********************************************************************************
What are the first 7 characters of the hash of the commit that introduced the bug? 18ed2ac1522
Congratulations, you have solved the level!

50

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
Name: stage_lines
Level: 50
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 git:(master) ✗ git status
On branch master
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: feature.rb

no changes added to commit (use "git add" and/or "git commit -a")
➜ git_hug git:(master) ✗ git add feature.rb -e

[删除第二个commit]

➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

51 - 56

51

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Name: find_old_branch
Level: 51
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 git:(kill_the_batman) git reflog
➜ git_hug git:(kill_the_batman) git checkout solve_world_hunger
Switched to branch 'solve_world_hunger'
➜ git_hug git:(solve_world_hunger) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

52

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Name: revert
Level: 52
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 git:(master) git revert HEAD^1
[master e2a572b] Revert "Bad commit"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file3
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

53

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
Name: restore
Level: 53
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 git:(master) git reflog
➜ git_hug git:(master) git checkout ef65ada
Note: checking out 'ef65ada'.

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 ef65ada... Restore this commit
➜ git_hug git:(ef65ada) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

54

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
Name: conflict
Level: 54
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 git:(master) git branch
* master
mybranch
➜ git_hug git:(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 git:(master) ✗ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")

Unmerged paths:
(use "git add <file>..." to mark resolution)

both modified: poem.txt

no changes added to commit (use "git add" and/or "git commit -a")
➜ git_hug git:(master) ✗ vim poem.txt
➜ git_hug git:(master) ✗ git add poem.txt
➜ git_hug git:(master) ✗ git commit -m "solve conflict"
[master 6ab8003] solve conflict
➜ git_hug git:(master) githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

55

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: submodule
Level: 55
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 manually cloning the repo or copying the files from the repo into this repo.


➜ git_hug git:(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.
➜ git_hug git:(master) ✗ githug
********************************************************************************
* Githug *
********************************************************************************
Congratulations, you have solved the level!

56

1
2
3
4
5
Name: contribute
Level: 56
Difficulty: ***

This is the final level, the goal is to contribute to this repository by making a pull request on GitHub. Please note that this level is designed to encourage you to add a valid contribution to Githug, not testing your ability to create a pull request. Contributions that are likely to be accepted are levels, bug fixes and improved documentation.

End,鼓励contribute,提PR。