公司新来了一个和我同姓的棒棒后端咖~举办了两次关于 git 的技术分享,为了巩固提高还布置了作业,作业链接在此:Githug。我照着通关攻略不紧不慢的通了关并将此安利给了我只会 add commit push 的小男友)偷笑。唉,下午竟然还有考试呢,中午 HR 让我们临阵磨枪,我脑子里都是:临阵磨枪,再快也慌~ 那就温习下 githug ~~~~一个学习 git 的小游戏吧!
Githug 安装和使用方法
先将 github 上的 githug 项目 clone 到本地,安装好 ruby 的运行环境,接着 gem install githug,输入 githug 创建新目录,cd git_hug 切换到 git_hug 目录下开始闯关
闯关内部命令
1 | githug play:验证是否通过此次关卡,可省略play |
通关历程
第 1 关 init
命令:git init
第 2 关 config
命令:git config –add (–local/–global) user.name/user.email
第 3 关 add
1 | git add 文件名 |
第 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 | untracked:新建的工作区文件 |
第 10 关 number_of_files_committed
命令:同上
第 11 关 rm
命令:git rm 文件名:删除远程仓库文件
第 12 关 rm_cached
命令:git rm –cached 暂存区文件:撤销暂存区文件到工作区
第 13 关 stash
1 | git stash |
第 14 关 rename
命令:git mv 旧文件名 新文件名
第 15 关 restructure
命令:mkdir src、git mv *.html ./src/
第 16 关 log
命令:git log –pretty=oneline
第 17 关 tag
1 | git tag my-tag:给最近一次commit打tag |
第 18 关 push_tags
命令:git push –tags
第 19 关 commit_amend
1 | 先add |
第 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 | git push remote-name branch-name |
命令:git rebase(本地 commit 会排在远程 commit 之后)
第 29 关 diff
命令:git diff [文件名]
第 30 关 blame
命令:git blame 文件名:会列出每一行的 hash 值、作者、时间
第 31 关 branch
命令:git branch branch-name
第 32 关 checkout
1 | git checkout branch-name:切换分支 |
第 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 | git fetch |
第 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 | pick:执行此次提交 |
第 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
- Githug 安装和使用方法
- 通关历程
- 第 1 关 init
- 第 2 关 config
- 第 3 关 add
- 第 4 关 commit
- 第 5 关 clone
- 第 6 关 clone to folder
- 第 7 关 ignore
- 第 8 关 include
- 第 9 关 status
- 第 10 关 number_of_files_committed
- 第 11 关 rm
- 第 12 关 rm_cached
- 第 13 关 stash
- 第 14 关 rename
- 第 15 关 restructure
- 第 16 关 log
- 第 17 关 tag
- 第 18 关 push_tags
- 第 19 关 commit_amend
- 第 20 关 commit_in_future
- 第 21 关 reset
- 第 22 关 reset soft
- 第 23 关 checkout file
- 第 24 关 remote
- 第 25 关 remote_url
- 第 26 关 pull
- 第 27 关 remote_add
- 第 28 关 push
- 第 29 关 diff
- 第 30 关 blame
- 第 31 关 branch
- 第 32 关 checkout
- 第 33 关 checkout_tag
- 第 34 关 checkout_tag_over_branch
- 第 35 关 branch_at
- 第 36 关 delete_branch
- 第 37 关 push_branch
- 第 38 关 merge
- 第 39 关 fetch
- 第 40 关 rebase
- 第 41 关 rebase_onto
- 第 42 关 repack
- 第 43 关 cherry-pick
- 第 44 关 grep
- 第 45 关 rename_commit
- 第 46 关 squash
- 第 47 关 merge_squash
- 第 48 关 reorder
- 第 49 关 bisect
- 第 50 关 stage_lines
- 第 51 关 find_old_branch
- 第 52 关 revert
- 第 53 关 restore
- 第 54 关 reflict
- 第 55 关 submodule
- 第 56 关 contribute