Githug-通关笔记

前言
目前公司使用的是svn,Cornerstone客户端,还是破解版的(逃。。)除了基本的add、commit、解决一下config、打个tag,很少用到别的功能。而我私下比较喜欢使用git。忽然在V2发现了 githug 的通关游戏,一共55关,赶紧开撸。

安装

安装很简单

1
gem install githug

如果权限不够,前面加上sudo。然后cd进一个文件夹,输入githug,会有提示你是否创建一个游戏目录,选

name是关卡名字,level是第几关,difficulty是难度,最下面是关卡介绍。

游戏命令
  • githug play 检查是否过关
  • githug hind 当前关卡提示
  • githug reset 重启当前关卡或者重启到指定名字的关卡
  • githug levels 所有关卡列表
开始闯关

先来一个备忘以备不时之需

TIPS: 每一个关卡都是一个新的repo, 不要想在前一个的文件中找什么

1. init

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

初始化一个本地仓库

1
2
cd git_hug
git init

2. config

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

设置名字和邮箱

1
2
git config user.name "yourname"
git config user.email "youremail"

3. add

here is a file in your folder called README, you should add it to your staging area

你的git_hug文件夹下生成了一个README,把他加到缓存区

1
git add README

4. commit

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

已经加入缓存区,现在把它提交

1
git commit -m"commit readme"

5. clone

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

克隆一个远程库

1
git clone https://github.com/Gazler/cloneme

6. clone_to_folder

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

克隆一个远程库到指定文件夹

1
git clone https://github.com/Gazler/cloneme my_cloned_repo

7. ignore

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.

使用vim编辑,忽略.swp文件。添加.gitignore文件就好了。

1
2
vi .gitignore
然后插入 *.swp

关于.gitignore的更多用法就不多做介绍了,先闯关!

8. include

Notice a few files with the ‘.a’ extension.We want git to ignore all but the ‘lib.a’ file.

忽略所有.a,但是不忽略lib.a

1
2
3
vim .gitignore
插入 *.a
!lib.a

9. status

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

有一个文件没有加到缓存区。查看状态即可,找到Untracked files下的文件

1
git status

10. number_of_files_committed

There are some files in this repository, how many of the files will be committed?

看看有哪些文件将要提交,依然查看状态,找到to be commited下面的文件

1
git status

11. rm

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.

在目录中删除但是库里没删,找到并删掉它。查看状态找到deleted的文件,然后删除

1
2
git status
git rm deleteme.rb

12.rm_cached

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删除哦。

1
2
git status
git rm ––cached deleteme.rb

13. stash

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

把你现在做的一些修改暂存起来,不提交

1
git stash

14. rename

We have a file called oldfile.txt. We want to rename it to newfile.txt and stage this change.

改个名

1
git mv oldfile.txt newfile.txt

15. restructure

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.

把所有.html文件都加入到新建的src文件夹中

1
2
mkdir src
git mv *.html src/

16. log

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

通过日志查看commit历史记录的哈希值。

1
git log

17. tag

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

给这次repo打个标签

1
git tag new_tag

18. push_tags

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

把这些tags推到远程库

1
git push --tags

19. commit_amend

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.

readme已经提交了,但是有个文件没有提交,在你上一次提交里加上它
可以使用 git commit –amend 修改上一次的提交信息,前提是你还没有push。

1
2
git add forgotten_file.rb
git commit ––amend

20. commit_in_future

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

提交的时候加上未来的一个日期

1
git commit --date=2016.01.01 -m"add date"

21. reset

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).

有两个要被提交的文件,想要分开提交,但是已经把他们一起加入到了缓存区,现在把其中的一个文件撤销出来

1
git reset HEAD to_commit_second.rb

22. reset_soft

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

提交的太快,现在想要取消最后一次提交。加上 –soft参数可以使撤销的提交仍然在缓存区,不需要再add

1
git reset ––soft HEAD^

23. checkout_file

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

一个文件被修改了,但是你不想要这个更改。从最后一次提交中把这个文件清掉。

1
git checkout config.rb

24. remote

This project has a remote repository.Identify it

显示远程仓库名

1
git remote

25. remote_url

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

显示远程库的url

1
git remote -v

26. pull

You need to pull changes from your origin repository.

把远程库的改动拉取到本地

1
git pull origin master

27. remote_add

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

添加一个远程库

1
git remote add origin https://github.com/githug/githug

28. push

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

你本地的master分支和远程的master分支都进行了提交然后发生了分叉,合并之后进行推送.

1
2
git rebase
git push

29. diff

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

上次提交完这个文件后发生了一些改变,找到哪行变了

1
git diff

30. blame

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

有人在这个文件加了密码,找到这个人

1
git blame 'config.rb'

31. branch

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

创建一个分支

1
git branch test_code

32. checkout

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

创建并选取一个新的分支

1
git checkout -b my_branch

33. checkout_tag

You need to fix a bug in the version 1.2 of your app. Checkout the tag v1.2.

切换到指定tag

1
git checkout v1.2

34. checkout_tag_over_branch

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).

切换到指定tag,注意:有一个分支名和tag名相同

1
git checkout tags/v1.2

35. branch_at

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.

提交了代码到版本库才记起要在前一个版本创建分支。

1
2
git log
git branch test_branch -v eee491beb

36. delete_branch

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

删除分支

1
git branch -d delete_me

37. push_branch

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

推送到远程的指定分支

1
git push origin test_branch

38. merge

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

把指定分支合并到主分支,先检查下现在在哪个分支

1
2
git branch
git merge feature

39. fetch

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

不用merge得到已经提交到远程版本的改变

1
git fetch origin

40. rebase

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.

feature分支已经准备好被合并进master,用rebase把它们合并

1
git rebase master feature

41. repack

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

这道题画个问号,用git repack失败,用git gc成功,但是结果是一样的判定不同。

1
git gc

42. cherry-pick

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版本库中存在两个分支,当我们想将其中的某个分支commitID应用到另一个中,我们可以使用git cherry-pick commitID。这里有个cherry-pick的介绍。

1
git cherry-pick ca32a6d

43. grep

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

项目期快到了,找到你项目中有多少个TODO

1
git grep TODO

44. rename_commit

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

重命名第一次提交的信息,找到那次提交之后进行修改

1
git rebase -i HEAD~2

45. squash (存疑)

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

有一些提交是相同的,合并为一个。找到了相同的前三次请求,然后把其中两次squash之后,log显示的也是合并了。不知道为何判定失败

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 rebase -i HEAD~3
// 合并了最近的三次。
pick 2023bbb Updating README
squash 3e06395 Updating README
squash fbc812d Updating README
// 原来有五次提交,现在只有三次
commit 2ae48a6462c88f5d7edb94fa222cc7cdb9dc1e3e
Author: chaoxn <chaoxn@gmail.com>
Date: Wed Jan 6 16:52:26 2016 +0800
Updating README
commit 47ecb3e5756c98b0d321eb45da7f80a207c89e94
Author: chaoxn <chaoxn@gmail.com>
Date: Wed Jan 6 16:52:26 2016 +0800
Adding README
commit 2c5300e327692e5c991ba5392b22660fedc81a6d
Author: chaoxn <chaoxn@gmail.com>
Date: Wed Jan 6 16:52:26 2016 +0800
Initial Commit

46. merge_squash

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

合并分支时,把分支的多次提交合并成一次提交

1
2
git merge --squash long-feature-branch
git commit -m"merge_squash"

47. reorder

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

提交顺序错了,重新提交。 把第二次和第三次交换一下顺序。

1
git rebase -i HEAD~3

48. bisect

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,通过运行 ruby prog.rb 5 查看输出是否是15来找出这个bug提交。通过git bisect来一次次的试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> git bisect start master [12628f4]
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> git bisect good f608824 master [12628f4]
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> git bisect bad master master [12628f4]
Bisecting: 9 revisions left to test after this (roughly 3 steps)
[fdbfc0d403e5ac0b2659cbfa2cbb061fcca0dc2a] Another Commit
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> ruby prog.rb 5 fdbfc0d [fdbfc0d]
15
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> git bisect good // // 在这里根据返回结果来判断下一步是good还是bad,如果是good会自动跳到下一次提交 fdbfc0d [fdbfc0d]
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[18ed2ac1522a014412d4303ce7c8db39becab076] Another Commit
fizz@fizzdeMacBook-Pro:~/Githug/git_hug
> ruby prog.rb 5 18ed2ac [18ed2ac
11 //这次提交是有bud的

49. stage_lines

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.

更改了一个文件中属于两个功能的东西,但是都还没有加入的缓存区。add只属于第一个功能的更改.

1
git add -p //进去之后把属于第二个feature的改动删掉。

注:交互式add(或者叫add块),将会一个块一个快的循环你的改动。使用命令git add -p时,你可以在每个改动“块”(即:连续的改动会被组织到一起)时进行一些选择,比如:切分当前块为更小的块、跳过一个改动块、甚至手动的编辑该块,你可以敲入?来查看所有该命令提供的选项。

50. find_old_branch

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.

在一个新的分支干活,想起上个分支有些问题,但是你还忘了上个分支的名字。通过reflog查看所有分支记录,然后找到上次切换的那个改动,找到分支名。

1
2
git reflog
git checkout solve_world_hunger

51. revert

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

已经提交了好几次,但是不想要中间那次了,所有的提交都push了,所以你不能改变已经存在的历史。同样,reflog找到,然后revert。

1
2
git reflog
git revert df2c0c6

git revert 是撤销某次操作,此次操作之前的commit都会被保留
git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区

52. restore

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.

把最后一次提交撤销了之后现在后悔了

1
2
git reflog
git checkout b6a7d40

53. conflict

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.

合并分支的时候有冲突,解决冲突。

1
2
3
4
5
6
git branch
git merge mybranch
git status
vim poem.txt
git add poem.txt
git commit

54. submodule

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.

从远程把一个项目整到本地某个文件夹中,不要clone或者copy。

1
git submodule add https://github.com/jackmaney/githug-include-me ./githug-include-me

注:git Submodule 是一个很好的项目协作工具,他允许类库项目做为repository,子项目做为一个单独的git项目存在父项目中,子项目可以有自己的独立的commit,push,pull。而父项目以Submodule的形式包含子项目,父项目可以指定子项目header,父项目中会提交 Submodule的信息,在clone父项目的时候可以把Submodule初始化。

55. contribute

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.

1
nothing to say