Git Game: Copying and Pasting From Documentation

文章目录

考试月的预习阶段不想看书(毕竟不到考前最后一个晚上怎么有动力学习呢),然后发现了这个东西: git-game,就当做是休闲一下了(逃

首先当然是要 clone 这个 Repo 并转到对应目录下:

$ git clone https://github.com/git-game/git-game.git
$ cd git-game

在 Level 1 开始首先查看 README.md 的提示,在每次操作后应该再次查看这个文件确认是否到达了下一个 Level 并且按照提示继续游戏,即:

$ cat README.md

文章格式约定

对于每一个 Level,首先是 <h3> 大小的标题,之后是关于这一个 Level 的简介,以两条分隔线作为和其他内容的区分,然后一堆 xjb 乱写最后用粗体的 Solution 和相关代码给出进入下一个 Level 的方式

Level 1


Your first task is to checkout the commit whose commit message is the answer to this question:

When a programmer is born, what is the first thing he/she learns to say?


那显然是 Hello World! 啦,用:

$ git log

查看一下提交记录就可以找到对应的提交 ID,然后我们只需要回退到那个版本就可以进入 Level 2 了

Solution

$ git reset --hard 6402738

Level 2


We want to get to a branch whose name is the answer to this riddle:
I am a creature that is smaller than man, but many times more in number.
In code, my appearance can be subtle and no matter where I am found, I am unwanted.

What am I?


当然是 bug 啦,根据要求切换到 bug 分支即可

Solution

$ git checkout bug

Level 3


Sometimes we like to blame others for introducing bugs in our code.
Think you can find out who introduced a bug into our file cool.cpp?
We think he had something to do with the development of git.
And from what we hear he also made a branch under his name.
Checkout to that branch after you find out who the culprit is.

If you are still confused, this link might help


git blame 查看 cool.cpp 文件中每一行代码的提交者、时间等信息,然后找出那行导致 bug 出现的代码的提交者

$ git blame cool.cpp

根据显示的信息发现是 LinusTorvalds2014 最后更新这个文件并且加了个死循环???这名字不错啊?
然后用 checkout 进入同名的分支即可

Solution

$ git checkout LinusTorvalds2014

Level 4


The next clue you are looking for --
is in a file you choose to ignore!


嗯……提示信息在 .gitignore 文件里……然后查看文件发现直接就显示 Level 5 了,喵喵喵?

Solution

$ cat .gitignore 

Level 5


This file is hidden by default,
but did you know you have some branches that aren't shown to you,
when you check the list of branches?

For your next clue...

Which abstract data type tends to implement sets and maps??

The answer is the same answer to this riddle:

I am both mother and father.
I am seldom still
yet I never wander.
I never birth nor nurse.
What am I?

Afterwards... well, you know, checkout to the answer.


这是上一关的 .gitignore 文件里的说明,稍微改了一下格式 谜语的答案显然是树,也就是 tree 这个分支,所以切换进去就可以了

Solution

$ git branch tree

Level 6


Welcome to the "tree" branch.
Looks like good ol' Linus modified the "nextclueinput.cpp" file.
Normally, when ran with the shell script "outputclue.sh", the "nextclue
input.cpp" file would give us the next hint.

Maybe, you should try running the shell script with the "nextclue_input.cpp" file and see what happens...

You can run the script by running the command "./outputclue.sh FILE" .
If you are on Windows, it's okey to use git-bash that is installed with msysgit.


……按照提示做,对 nextclue_input.cpp 运行 outputclue.sh 进入 Level 7

Solution

$ ./outputclue.sh nextclue_input.cpp

Level 7


Linus has been here... I love messing with these amateur programmers!!
If you want some real fun, then you should try resolving a conflict between this branch (tree) and code4life.
I introduced a little bug that you should fix in the conflict. >:)
After you merge these 2 files you should run the shell script again!!

Good Luck!!!

Hint: https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line/


把当前的 tree 分支和 code4life 分支进行合并,然后处理冲突

$ git merge origin/code4life

git 提示:

Auto-merging nextclueinput.cpp CONFLICT (content): Merge conflict in nextclueinput.cpp
Automatic merge failed; fix conflicts and then commit the result.

也就是需要手动修改 nextclue_input.cpp 这个文件:

$ vim nextclue_input.cpp

可以看到文件内容如下,其中 <<<<<<<=======>>>>>>>三行都是 git 的区分提示:

#include <iostream>

using namespace std;  
//Level 9
int main()  
{
<<<<<<< HEAD

    cout << "This file was useless, so I made it better..." << endl;
    while(1);
=======
    cout << "hope you resolved all your conflicts!!" << endl;
>>>>>>> origin/code4life
    return 0;
}

删掉多余的代码,即提示行和 while(1); 行,变成这样:

#include <iostream>

using namespace std;  
//Level 9
int main()  
{
    cout << "This file was useless, so I made it better..." << endl;
    cout << "hope you resolved all your conflicts!!" << endl;
    return 0;
}

既然解决完了冲突就应该提交然后再次运行那个脚本了:

$ git add nextclue_input.cpp
$ git commit -m "Conflict Resolved"
$ ./outputclue.sh nextclue_input.cpp

输出结果如下:

Well, congratulations!! You fixed my conflict!! If you would like to continue, then you should checkout to the mouse branch!!

接下来要进入 mouse 分支,整理一下操作过程:

Solution

$ git merge origin/code4life

$ vim nextclue_input.cpp

$ git add nextclue_input.cpp
$ git commit -m "Conflict Resolved"

$ ./outputclue.sh nextclue_input.cpp

$ git checkout mouse

Level 8


Looks like you resolved your conflict and found our branch, congrats!!

Hmm...it seems this branch has a file that was seen before in another branch.
Do you "remember" what it is?
I think this file has something to do with the next clue, but it seems to be very ugly looking.
Maybe if we compare the "diff"erences between this file and the file from before we'll know where to go next...


bug 分支有 remember 这个文件,查看一下两个分支中这个文件的不同:

$ git diff bug remember

整理一下 git diff 给出的结果,如下:

The next clue is: In a branch named: Henry

Solution

$ git checkout Henry

Level 9


If you're looking for my branch then you have gone the wrong way!!

How do you checkout to a branch that has the same name as a tag???
Deal with the tag first!!


tag 名和 branch 名一样的情况下,使用 checkout 实际上并不是进入那个分支而是对 tag 起作用,所以要先删除掉这个 tag 然后重新进入该分支

Solution

$ git tag -d Henry
$ git checkout Henry

Level 10


Welcome!! It looks like you made it to my Branch!!!
Generally you want to refrain from making tags the same name as branches, unless you have a good reason.
The tag is more like the stable release.
While the branch is more like the in progress feature, which will be added soon.

You're almost done!! Excited?? Hope you are! You have one more thing to do!

Now its time to update the master branch, updating is really useful when you fork a repository and your forked repo starts to get behind on commits. The repository to update from is: https://github.com/drami025/git-game.git

Don't cheat!!

Here is a link that explains how to fork a remote repository.


把那个远程仓库 pull 下来……然后就行了……吧?

Solution

$ git pull https://github.com/drami025/git-game.git

似乎已经是最后一个 Level 了,查看一下 README.md

Git Game Finish Line

If you did not update the main git-game repository, then we are disappointed in you!!

However, if you did, then great Job!!
You completed our Git Game!

Well you:

  • viewed previous commits using "git log"
  • traversed to previous commits
  • checked out to different branches
  • ran "git blame" to see who made changes to a file
  • ran the diff command to see differences between branches
  • saw what .gitignore included and how it works
  • resolved merged conflicts
  • saw issues with naming tags and branches the same name
  • updated a local repository from a remote repository

Version control systems like git are extremely important tools to learn and use, Especially when collaborating on projects with other developers. It is our hope that you continue to practice your git skills so that you can one day become the ultimate git master!

Thanks for playing!

完结撒花~

这个版本并不难,大部分都是直接跟着简介操作就行了,也都是一些比较常用的命令。

关于 git-game-v2 的文章在这里