Githug 功略

1 准备

安装 Githug gem install githug 1

安装完成后直接 githug 就可以开始游戏

git hint 可以看过关提示

git reset 可以重设当前关卡的文件

2 开始

2.1 初始化

cd git_hug
git init

2.2 设置用户名和邮箱

git config user.name "your name"
git config user.email "your email"

2.3 stage 一个文件

git add README

2.4 提交已经 stage 的文件

git commit -m "Commit README"

2.5 clone 一个已有的项目

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

2.6 clone 一个已有的项目,并重命名

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

2.7 忽略 vim 的.swp 文件

.gitignore 中添加

*.swp

2.8 忽略除 lib.a 外其它的 .a 文件

.gitignore 中添加

!lib.a
*.a

2.9 查看没有跟踪的文件

通过命令 git status 得知, database.yml 没有跟踪

2.10 查看有几个文件将要 commit

还是通过命令 git status 得知, rubyfile1.rbrubyfile4.rb 将被 commit,共 2 个

2.11 删除文件

通过命令 git status 得知,发现被删除为 deletme.rb

git rm deleteme.rb

2.12 将文件从 git 移除,但不删除文件

还是通过命令 git status 得知,要移除的文件是 deleteme.rb

git rm --cached deletme.rb

2.13 保存当前状态但不提交2

git stash

2.14 重命名文件

git mv oldfile.txt newfile.txt

2.15 用 Git 把指定文件(.html)移至目录(src)

mkdir src
git mv *.html src

2.16 查看提交记录

用命令 git log 查看提交历史记录,答案是 commit 后字符串的前 7 位

2.17 Tag 一个提交

git tag "new_tag"

2.18 更新远程库

git push

2.19 将新修改并入至上一次提交中

git add forgotten_file.rb
git commit --amend

2.20 提交至未来

git commit -m "Tomorrow" --date "2016/1/1"

2.21 unstage 一个文件

git reset HEAD to_commit_second.rb

2.22 取消上一次提交3

git reset --soft HEAD~1

2.23 恢复文件

git checkout config.rb

2.24 验证 remote 信息

用命令 git remote , 得到答案 my_remote_repo

2.25 查看 remote 链接

用命令 git remote -v, 得到答案 https://github.com/githug/not_a_repo

2.26 pull

git pull origin master

2.27 关联远程库

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

2.28 合并远程分支: rebase4

git rebase origin/master
git push

2.29 查看文件变动

用命令 git diff app.rb 查看文件变动

diff --git a/app.rb b/app.rb
index 4f703ca..3bfa839 100644
--- a/app.rb
+++ b/app.rb
@@ -23,7 +23,7 @@ get '/yet_another' do
   erb :success
    end
     get '/another_page' do
     -  @message = get_response('data.json')
     +  @message = get_response('server.json')
	erb :another
	 end

从而得知改动的是 26 行

2.30 查看具体文件行的作者

git blame config.rb 查看修改记录,密码在第 5 行,从而知道提交人是 Spider Man

2.31 创建分支

git branch test_code

2.32 创建并切换至新分支

git checkout -b my_branch

2.33 按 Tag 检出分支

git checkout v1.2

2.34 Tag 和其中一个分支名相同,按 Tag 检出

git checkout tags/v1.2

2.35 按版本号检出至新分支

git log 得到前分支的 hash 码为 1e75375

git branch test_branch -v 1e75375

2.36 删除分支

git branch -d delete_me

2.37 只提交特定分支到远程库5

git push -u origin test_branch

2.38 合并分支6

git merge feature

2.39 fetch

git fetch

2.40 合并分支: rebase7

git checkout feature
git rebase master

2.41 repack(优化库并删除冗杂包)

git repack -d

2.42 cherry-pick

git checkout new-feature 切换到 new-feature 分支

git log 得到修改 README.md 的 hash 码 ca32a6d

git cherry-pick ca32a6d

2.43 grep

用命令 git grep TODO 得知,共 4 个 TODO

2.44 修改提交信息

用命令 git rebase -i HEAD^^ 合并分支,得到以下信息,并做出修改

pick a7b50d4 First coommit => rework a7b50d4 First coommit 
pick 4f1036c Second commit

# Rebase 8a01aa1..4f1036c onto 8a01aa1 (2 command(s))
#
# Commands:
# C-c C-c  tell Git to make it happen
# C-c C-k  tell Git that you changed your mind, i.e. abort
# M-x previous-line move point to previous line
# M-x next-line move point to next line
# M-p      move the commit at point up
# M-n      move the commit at point down
# M-x git-rebase-show-commit show the commit at point in another buffer
# u        undo last change
# M-x git-rebase-kill-line drop the commit at point
#          pick = use commit
#          reword = use commit, but edit the commit message
#          edit = use commit, but stop for amending
#          squash = use commit, but meld into previous commit
#          fixup = like "squash", but discard this commit's log message
#          exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

保存修改,然后把 coommit 修改为 commit

2.45 合并多次提交

用命令 git log 得到三次是同一提交的信息

用命令 git rebase -i HEAD~3 得到以下信息,进行修改

pick 3d8ba51 Updating README
pick 0683738 Updating README => squash 0683738 Updating README
pick bed1a85 Updating README => squash bed1a85 Updating README

# Rebase ead384a..bed1a85 onto ead384a (3 command(s))
#
# Commands:
# C-c C-c  tell Git to make it happen
# C-c C-k  tell Git that you changed your mind, i.e. abort
# M-x previous-line move point to previous line
# M-x next-line move point to next line
# M-p      move the commit at point up
# M-n      move the commit at point down
# M-x git-rebase-show-commit show the commit at point in another buffer
# u        undo last change
# M-x git-rebase-kill-line drop the commit at point
#          pick = use commit
#          reword = use commit, but edit the commit message
#          edit = use commit, but stop for amending
#          squash = use commit, but meld into previous commit
#          fixup = like "squash", but discard this commit's log message
#          exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

最后直接保存即可

2.46 把分支合并时仅保存一次提交

git merge --squash long-feature-branch
git commit -a -m "merge commit"

2.47 修改提交顺序

git log 得到信息:

commit eefb2f0fcb614559aa005cbd0ce70d2a1036a421
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:23:34 2015 +0800

    Second commit

commit b67bf29ed7ed7d34931a0e7259181c83ae874
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:23:34 2015 +0800

    Third commit

commit fa3f8d16b8c9559b4e8a35af7ee5cbbb58f5d61e
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:23:34 2015 +0800

    First commit

commit 55d49c869c98b23230c660d255cac1f3bd83
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:23:34 2015 +0800

    Initial Setup

用命令 git rebase -i HEAD~2 自己修改顺序

2.48 二叉树排错

用命令 git log -p progt.rb 查看最初第一次为正确提交,hash 码为 f608824

git bisect start
git bisect good f608824
git bisect bad
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
makefile:2: recipe for target 'test' failed
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

其中, 18ed2ac1522a014412d4303ce7c8db39becab076 is the first bad commit

所以答案为 18ed2ac

2.49 仅 stage 一个文件的其中一部分

打开文件得知 second feture 在第 3 行

用命令 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*

删除 +This change belongs to the second feature

2.50 查看操作历史

用命令 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

从而得知要切换到 solve_world_hunger 分支

git checkout solve_world_hunger

2.51 回滚某次提交历史

通过命令 git log 得到日志:

commit fffe6ae84f79432956b6d1be11e6fec9317c8931
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:48:08 2015 +0800

    Second commit

commit 27fa38ecd80951222b0f104455ce7abc0ea24e10
Author: cowlog <xx@xx.com>
Date:   Thu Dec 31 18:48:08 2015 +0800

    Bad commit

讲道理的话直接用命令 git revert HEAD~1 就可以了

可是它一直报错

直到看到这个issue, Orz….

2.52 挽救 git reset –hard HEAD的误操作

通过 git reflog 查看误操作前一步骤的 hash 码

git checkout 99db378

撤销误操作

2.53 合并分支并解决冲突

运行命令 git merge mybranch, 得到日志:

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

解决 poem.txt 的冲突,即编辑 poem.txt

Humpty dumpty
<<<<<<< HEAD //删除
Categorized shoes by color 
======= //删除
Sat on a wall
>>>>>>> mybranch //删除
Humpty dumpty //删除
Had a great fall

然后执行下面的命令

git add poem.txt
git commit -m "merge"

2.54 子模块

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

Date: <2015-12-31 Thu 21:40>

Author: cowlog

Created: 2016-03-23 Wed 21:30

Emacs 24.5.1 (Org mode 8.2.10)

Validate