githug通关

作者 zhufenfen 日期 2020-11-16
githug通关

公司新来了一个和我同姓的棒棒后端咖~举办了两次关于 git 的技术分享,为了巩固提高还布置了作业,作业链接在此:Githug。我照着通关攻略不紧不慢的通了关并将此安利给了我只会 add commit push 的小男友)偷笑。唉,下午竟然还有考试呢,中午 HR 让我们临阵磨枪,我脑子里都是:临阵磨枪,再快也慌~ 那就温习下 githug ~~~~一个学习 git 的小游戏吧!

Githug 安装和使用方法

先将 github 上的 githug 项目 clone 到本地,安装好 ruby 的运行环境,接着 gem install githug,输入 githug 创建新目录,cd git_hug 切换到 git_hug 目录下开始闯关
闯关内部命令

1
2
3
4
githug play:验证是否通过此次关卡,可省略play
githug hint:寻求提示,help、help、help、
githug reset:重置本次关卡,推倒重来~
githug levels:查看各自名称

通关历程

第 1 关 init


命令:git init

第 2 关 config


命令:git config –add (–local/–global) user.name/user.email

第 3 关 add

1
2
3
git add 文件名
git add *.md:扩展名为md的文件都添加到暂存区
git add ./-A:工作区文件都添加到暂存区

第 4 关 commit


命令:git commit -m “my commit message”

第 5 关 clone


命令:git clone 远程仓库地址

第 6 关 clone to folder


命令:git clone 远程仓库地址 自定义目录名

第 7 关 ignore


命令:vim .gitignore、i、输入*.swap、esc、:wq、

第 8 关 include


命令:vim .gitignore、i、输入*.a、回车、输入!lib.a、esc、:wq

第 9 关 status


命令:git status (-s)

1
2
3
untracked:新建的工作区文件
modified:修改的工作区文件
staged:暂存区文件

第 10 关 number_of_files_committed


命令:同上

第 11 关 rm


命令:git rm 文件名:删除远程仓库文件

第 12 关 rm_cached


命令:git rm –cached 暂存区文件:撤销暂存区文件到工作区

第 13 关 stash

1
2
3
git stash
git stash list
git stash pop

第 14 关 rename


命令:git mv 旧文件名 新文件名

第 15 关 restructure


命令:mkdir src、git mv *.html ./src/

第 16 关 log


命令:git log –pretty=oneline

第 17 关 tag

1
2
3
4
git tag my-tag:给最近一次commit打tag
git tag my-tag hash:给指定commit打tag
git tag:查看所有tag
git tag -d my-tag:删除指定tag

第 18 关 push_tags


命令:git push –tags

第 19 关 commit_amend

1
2
3
4
先add
git commit --amend -m "new message":替换原有的commit说明
git commit --amend -C HEAD:使用最近一次的commit 说明
git commit --amend:自行编辑提交说明

第 20 关 commit_in_future


命令:git commit –date=”具体日期/10 minutes ago/last friday/noon yesterday” -m “commit message”

第 21 关 reset


命令:git reset 文件名

第 22 关 reset soft


命令:git reset –soft HEAD^:撤销最后一次 commit 到暂存区

第 23 关 checkout file


命令:git checkout 文件名:放弃工作区的某个文件

第 24 关 remote


命令:git remote:查询连接了哪些远程仓库

第 25 关 remote_url


命令:git remote -v:查询远程仓库地址(包含 fetch 和 push)

第 26 关 pull


命令:git pull remote-name branch-name

第 27 关 remote_add


命令:git remote add remote-name remote-url

第 28 关 push

1
2
3
git push remote-name branch-name
git push -u remote-name branch-name(记住仓库名和分支名)
git push(记住之后下次直接可以输入此命令)

命令:git rebase(本地 commit 会排在远程 commit 之后)

第 29 关 diff


命令:git diff [文件名]

第 30 关 blame


命令:git blame 文件名:会列出每一行的 hash 值、作者、时间

第 31 关 branch


命令:git branch branch-name

第 32 关 checkout

1
2
3
git checkout branch-name:切换分支
git checkout -b branch-name:创建并切换分支
git chekcout -:切换到上个分支

第 33 关 checkout_tag


命令:git checkout tag-name

第 34 关 checkout_tag_over_branch


命令:git checkout tags/tag-name

第 35 关 branch_at


命令:git branch branch-name hash-code

第 36 关 delete_branch


命令:git branch -d branch-name

第 37 关 push_branch


命令:git push remote-name branch-name

第 38 关 merge


命令:git merge branch-name

第 39 关 fetch

1
2
3
git fetch
git branch -r:查看远程分支
git log remote-name/branch-name:查看远程分支log

第 40 关 rebase


命令:git checkout feature、git rebase master 当前分支的 commit 会在最上面

第 41 关 rebase_onto


命令:git rebase –onto master wrong_branch

第 42 关 repack


.git 用来存放 Git 管理仓库要用到的文件
命令:ls .git/objects/、git repack -d(打包并删除作废的对象)

第 43 关 cherry-pick


命令:git cherry-pick hash-code:选取其中一条提交到主线

第 44 关 grep


命令:git grep keyword [file-name]:在当前项目/指定文件中查找关键词

第 45 关 rename_commit


命令:git rebase -i 前一条 hash-code、reword

1
2
3
4
pick:执行此次提交
reword:执行此次提交,但要修改备注内容
squash:合并此次提交
drop:删除此次提交

第 46 关 squash


命令:git rebase -i 前一条 hash-code、squash

第 47 关 merge_squash


命令:git merge branch-name –squash

第 48 关 reorder


命令:git rebase -i 前一条 hash-code、调整顺序

第 49 关 bisect


第 50 关 stage_lines


命令:git add file-name –edit

第 51 关 find_old_branch


命令:git reflog:可以查看在 git 上的历史操作

第 52 关 revert


命令:git revert bad-hash-code [–no-edit]自动生成一个提交信息

第 53 关 restore


命令:git reflog、git reset –hard hash-code

第 54 关 reflict


命令:git merge branch-name、

第 55 关 submodule


命令:git submodule add module-url

第 56 关 contribute


恭喜通关!!!

总结与提高

考试成绩下来了)滑稽

再来看看另一个同学的,正所谓,没有对比,就没有开心)再次滑稽

如何删除倒数第二次提交(未提交到远程)?
答:见第 45 关的 drop 或 调整下倒数第一和倒数第二的顺序然后 reset 一次
你的提交已经 push 到远程了,删除倒数第二次提交
答:上题一顿操作后 push -f(目前还没想到其它比较好的方法)
你在 HCM-888 的分支上开发,然后 merge 了 master,上线后发现业务有问题,QA 同学执行了回滚,你需要在原分支代码基础上修复 bug 上线,请写出操作流程
答:在 HCM-888 分支上 merge 下远程 master 分支,再 git revert hash-code(master 上的回滚操作),然后修复 bug