您当前的位置:首页 > 计算机 > 软件应用 > 开发工具(IDE)

透过 git 有效率地练功

时间:12-14来源:作者:点击数:
CDSY,CDSY.XYZ

你会在这篇得到

  • 一个完整的情境,初学者如何使用 Git 来临摹帖子并反覆练习。
  • 有了作品后,如何上传到 GitHub

个人建议遇到问题时再去查书,不要从第一页看到最后一页,这样你学不会,看完也忘光光。

临摹写 code

有效率地透过git来练习写code。

这篇教学以临摹TreeHouse的教材为例。我推荐用 TreeHouse 入门 Web(link:https://github.com/NickWarm/Articles/blob/master/posts/%E7%82%BA%E4%BD%95%E6%88%91%E6%8E%A8%E8%96%A6TreeHouse.md),TreeHouse 都会提供课程的 code,会有startfinal,copy一份start后,改个专案名字。

把专案初始化

git init

如果要自己架 Git Server 跟别人协同开发,JC大建议(link:http://tienshunlo-blog.logdown.com/posts/711614-novice-instruction-day2-hd" rel="nofollow) 初始化写 ( 新手不需要用这方法 ),详情请见 Git - 架设伺服器(link:https://git-scm.com/book/zh-tw/v1/%E4%BC%BA%E6%9C%8D%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E6%9E%B6%E8%A8%AD%E4%BC%BA%E6%9C%8D%E5%99%A8" rel="nofollow)。

git init --bare project.git

接著照著课程打 code

每段影片都会有数个步骤,第一次写时,我会每一个小步骤都

git add .
git commit -a -m "....."   /*不进入vim的写法

这是程式技术最有效的学习方法(link:http://sdlong.logdown.com/posts/736307" rel="nofollow)所採取的方法,我会在临摹他人的帖子如何做笔记(link:https://github.com/NickWarm/Articles/blob/master/posts/%E8%87%A8%E6%91%B9%E4%BB%96%E4%BA%BA%E7%9A%84%E5%B8%96%E5%AD%90%E5%A6%82%E4%BD%95%E5%81%9A%E7%AD%86%E8%A8%98.md)这篇文章中解释,实作 sdlong 的学习法后,为何我不再使用 每一个小步骤都 git commit送出一个 patch,他背后的理由与修正后的笔记方法。

上面这行可以简写做

git commit -am "....."

若是对commit的内容不满意,可以透过下面这指令来修改最新的 commit

git commit --amend

下完 --amend 指令后,会进入vim编辑器,一开始我们是无法进行编辑的,入要编辑要按下

i

接著,把commit修改成我们希望的内容,完成编辑后,我要要退出编辑,按下

esc

最后,我们要储存并离开,按下

:wq

如果不想进到vim去改也可以透过

git commit --amend -m "message"

若是我们对现在的进度不满意,想要回到以前的纪录(4),首先我们要看过去的纪录

git log

下完指令后,我们可以看到过去个版本的标记代码,如果你只想看到最新的三笔资料,可以下

git log -3

以此类推

下指令,回到过去的版本

git checkout <过去版本代码>

现在我们回到过去了,如果我们想回到最新版本,就下

git checkout <最新版本的代码>

回到过去某一版,并把最新版的纪录都消掉,就下

git reset --hard  <过去版本的代码>

相关链接

  • Commit --amend【教学3 改写提交】(link:https://backlogtool.com/git-guide/tw/stepup/stepup7_1.html" rel="nofollow)
  • 鸟哥的 Linux 私房菜 -- 第九章、vim 程式编辑器(link:http://linux.vbird.org/linux_basic/0310vi.php" rel="nofollow)
  • Git Magic - 章 2. 基本技巧 - 进阶撤销/重做(link:http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_tw/ch02.html#_%E9%80%B2%E9%9A%8E%E6%92%A4%E9%8A%B7_%E9%87%8D%E5%81%9A" rel="nofollow)
  • Git Reference - checkout(link:http://gitref.org/branching/#branch" rel="nofollow)

再练习一遍

照著教学,我们写完一遍后,接著我想要再练习一遍

首先我用git log找到我的练习想要开始的地方,并用git checkout跳回去,接著我再开一个新分支

git checkout -b (branchname)

然后我就能练到爽了

如果你同个练习已经开数个分支练太多遍了,想要把多馀的分支砍掉,可以下

git branch -d (branchname)

merge

写一个新功能就开一个branch,然后再merge之。

一般来说,你要commit到production的branch都在master,现在你要开一个新branch,可以下

git checkout -b (写新功能的branchname)

你就会跳到新branch,然后尽情地写你要的功能,不用怕过去的进度被搞坏,当你写好新功能后,跳回master branch

git checkout master

接著我们要把新写好的功能加到master去

git merge (写新功能的branchname)

功能加好后,你就能把新开的分支砍掉

git branch -d (branchname)

Conflict

merge 后,有时候 master branch与develop branch 会有衝突,这时就要修掉 conflict

我习惯的开发环境 Atom 是使用 merge-conflicts(link:https://atom.io/packages/merge-conflicts" rel="nofollow) 这套 package。

当你用 git commit 送出patch,发现 log 跟你说有衝突,于是回到 Atom

在 Atom 中下cmd + shift + p 下merge c然后选择Merge Conflicts: Detect

接著你会在底层看到

merge_conflict1

他会跟你说哪裡有衝突,点选任意一项后,就会跳到有衝突的地方

merge_conflict2

把你不想要的地方修掉后,剩下的那块依旧会用颜色显示,点选use me

解完衝突后回去iTerm下git add

更多merge conflict的知识请参阅Cherry-Pick 版本衝突(link:https://zlargon.gitbooks.io/git-tutorial/content/patch/cherry_pick_conflict.html" rel="nofollow)

相关链接

  • merge-conflicts(link:https://atom.io/packages/merge-conflicts" rel="nofollow)

临摹他人的 GitHub projects

我们可以从他人的专案去学习,首先到他人专案的GitHub,点选 Clone or download

1

接著把这段网址複制下来,先用iTerm进入到你想要下载的路径,然后下指令

git clone [ 複制下来的网址 ]

这样就能把人家的专案与 commit 纪录都複制下来了。

个人建议用 git clone 来载他人的专案最恰当,如果你用 Download Zip,虽然也可以把人家的专案载下来,但是这载下来的专案没有任何 Git commit,这对我们的学习是没有帮助的。

上传到 GitHub

你写好的专案可以上传到GitHub,open source出来

在 Github 新增专案

  • 按右上角的 +,选择 New repository
  • 输入 Repository name
  • 这裡先不要勾选 "Initialize this repository with a README"
  • 按 Create repository

新增完成后,Github 会提供你 Repository 的 URL

设定环境,使专案能上传 GitHub

接著我们下指令生成SSH Key

$ ssh-keygen -t rsa

下完指令后,会出现几个问题,这裡只要一直按 enter 就行了

接著将公钥id_rsa.pub上传到 Github Server

点选你的GitHub个人页面 > Settings > SSH Keys

点右上角的Add SSH key

把 ~/.ssh/id_rsa.pub 的内容複制贴上,点选 "Add key" 送出

测试 SSH,下指令

$ ssh -T git@github.com

初次连线 Github Server 可能会出现以下讯息,输入 yes 即可

The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)? yes
2

相关链接

  • 新增专案(link:https://zlargon.gitbooks.io/git-tutorial/content/remote/new_project.html" rel="nofollow)
  • 设定 Repo URL(link:https://zlargon.gitbooks.io/git-tutorial/content/remote/remote.html" rel="nofollow)

上传到 GitHub

在你要上传的专案底下,下这道指令,设定你要传到哪去

git remote add github [你的 Repository 的 URL]

然后下这道指令,上传 GitHub

git push github master

相关链接

  • 设定 Repo URL(link:https://zlargon.gitbooks.io/git-tutorial/content/remote/remote.html" rel="nofollow)
  • 上传分支(link:https://zlargon.gitbooks.io/git-tutorial/content/remote/push.html" rel="nofollow)

推荐阅读

  • 我是如何学一门新技术的(link:http://blog.xdite.net/posts/2016/07/09/how-did-i-learn-a-new-skill" rel="nofollow)
  • Git · GitBook(link:https://zlargon.gitbooks.io/git-tutorial/content/index.html" rel="nofollow)
  • [实战心得] Git 与 Github 版本控制(link:http://sdlong.logdown.com/posts/171365" rel="nofollow)
  • [心得分享] 程式技术最有效的学习方法:认真仔细的整理 git commit(link:http://sdlong.logdown.com/posts/736307" rel="nofollow)
  • [心得分享] 用 git rebase 来整理 commit 的技巧与概念(link:http://sdlong.logdown.com/posts/736902-sharing-organize-commit-in-git-rebase-to-techniques-and-concepts" rel="nofollow)
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐