that's (g)it.-
git 就是這麼一回事
Ming-der Wang, 王銘德

TurboTeam 集先鋒
1991 - 2002
用 email 寄 tarballs, 或用 patches. (兩年前, 我還看過)
2005
濱野 純 維護 git source code.
一開始是 Linux kernel 版控在用.
2007
五月, git 發明人 Linus Torvalds 到 Google 演講, 介紹什麼是 git
2016
11 年後的今天...
(我們在這裡上 git 的課)
1990
CVS (26 年前開始, 2008 穩定)
SVN (16 年前開始, 2016 穩定)
2002 - 2008
bitkeeper, rcs -> cvs -> svn
2008 我們 KatDC 開始用 git
版本控制系統的演進
SVN
集中式
大部分的公司還在用
沒有什麼不好
team 在用, 我只好跟著用
絕對不要再用 tarballs 就好!!
Git
分散式集中
大部分會比較小
分散, 造成比較可靠
自由度比較高
=> 比較 agile 一點
- 大部份時候 Git 比較快
大部分時候 Git 比較快

<但我覺得這都還不是重點, agile 才是重點>
Git = agile programming
SVN 沒有不好, git 也沒什麼了不起. 但我想分享的經驗是
git 比較不需要跟別人的 source code 等來等去...
<以最常見的情況 hotfix 為例>
解說 hotfix 作法
https://github.com/cetic/git-slides/blob/master/presentations/2013-11-26-FeWeb/git-feweb-2013-en.pdf?raw=ture
先下載 git-feweb-2013-en.pdf 檔
Git branching model
<Git 的另一個好處是 "開 branches 不用錢">
* local 你可以盡量開 branches

典型版控模式
Git 的分散式集中

local
local
local
local
remote
注意: 每個人會有一份 local copy
而且可能不樣的 branches
我們稱這 origin 為 "remote"
- 通常只有一個 origin remote
- 但我們也可以有多個 remote (平常比較少這樣用)
- 因此我們每個人的 local 也未必跟別人相同
Gitflow 是啥?
公司專案, 要如何分 branches?
* 有一個主枝幹 (master)
* 通常不再主枝幹上開發
* 只有在主枝幹上出版(加 tags)
* 開發大主幹 (develop)
* 以增加新功能命名分枝
* 以未來大新版命名分枝
* 以改 bug 問題命名分枝
* hotfixes (熱修改) <- 說明
click here
開始學 git 指令
常用 Git 指令 - 16 個就夠了
$ git clone
$ git init
$ git branch -a
$ git add -A
$ git commit -m '說明...'
$ git checkout
$ git remote -v
$ git push origin <branch_name> --tag
$ git pull origin <branch_name> --tag
$ git tag
$ git log
$ git status
$ git diff
$ git merge --no-ff
$ git stash, git stash pop
$ git rebase
常用 Git commands (一) clone
// 從 remote 端 clone 一個 project 過來
$ cd /tmp
$ git clone https://github.com/nodeapps/helloworld.git
$ cd helloworld
$ git log
$ git tag
v0.2.2
$ git pull origin master
常用 Git commands (二) init
cd /tmp
mkdir myproject
cd myproject
git init
vi README.md
git add -A
git commit -m 'add README'
git tag v1.0
git status
常用 Git commands (三) branch
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
$ git checkout -b develop


$ git checkout master

常用 Git commands (四) add
// 每次要 commit 程式前, 先把所有更新 add 進來 tracking
$ git status
$ git add -A
常用 Git commands (五) commit
// 每個 branch 上的圓球就是一個 commit
$ git status
$ git log
$ git commit -m '一行說明'
$ git log
// 每個 branch 都能 commit
// 盡量只在 features, hotfix
branches 上改 code.

常用 Git commands (六) checkout
// checkout 用來跳 local branch 的
$ git checkout -b new_feature develop
$ git checkout -b release-1.2
$ git branch -a
$ git checkout master
$ git checkout <tag_name>
// 注意: checkout tag_name 是臨時 branch name (例如 19b1a5b)

feature-1 feature 2

常用 Git commands (七) remote
$ cd /tmp/myfirstVertx
$ git remote -v
origin ssh://git@git.nkg-cloud.com:10022/mingderwang/myfirstVertx.git (fetch)
origin ssh://git@git.nkg-cloud.com:10022/mingderwang/myfirstVertx.git (push)
$ git remote add private git@redmine.log4analytics.com:/opt/git/myfirstVertx.git
$ git push private master --tag
常用 Git commands (七) 續
$ git remote -v
origin ssh://git@git.nkg-cloud.com:10022/mingderwang/myfirstVertx.git (fetch)
origin ssh://git@git.nkg-cloud.com:10022/mingderwang/myfirstVertx.git (push)
private git@redmine.log4analytics.com:/opt/git/myfirstVertx.git (fetch)
private git@redmine.log4analytics.com:/opt/git/myfirstVertx.git (push)
常用 Git commands (八) push
// 將 local 某個 branch 跟 remote 某個 branch 同步
$ git pull origin master
$ git push origin master
$ git checkout develop // 跳到 develop branch
$ git pull origin develop
$ git push origin develop
常用 Git commands (九) pull
$ git checkout master
$ git pull origin master
// git pull = git fetch + git merge
常用 Git commands (十) tag
$ git tag
$ git checkout <tag_id>
$ git checkout master
$ git tag v2.0
$ git tag
$ git tag -d v2.0 (刪除)
// 要把 tag 一起 push 到 server
$ git push origin master --tag

常用 Git commands (11) log
$ git log

常用 Git commands (12) status
// 看當時狀態

常用 Git commands (13) diff
$ git diff
$ git diff 1586e1f3b28374c599ce6555d01d1a0f7e96b643

常用 Git commands (14) merge
$ git checkout develop
$ git merge --no-ff myfeature
$ git branch -d myfeature
$ git push origin develop

常用 Git commands (15) stash
// 改到一半的 code
// 突然需要 hotfix 怎麼辦
$ git stash
// 暫存改到一半的 code
// 先做其他事
// 等做完事後,
$ git stash pop
// 回到原來狀態


常用 Git commands (16) rebase
把原本在 aff6f1b.. 的地方, 接到 v2.0 (branch) 後面, 用
git checkout aff6
git rebase v2.0

* 如果有任何 conflict (衝突)
改好後, git add/rm <conflicted_files>, 在做 git rebase --continue
That's (g)it
git 就是這麼一回事
+ 不斷得練習
所謂 conflict 就是需手動處理

保留其中之一
並刪掉這 3 行
第一次用 git commit 時
會要求你輸入姓名與 email address
例如:
$ git commit -m 'init'
*** Please tell me who you are.
須執行以下兩行, 用你的 email 和名字
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
換成你的名字和 email
(這只要做一次 )
Q&A
mingderwang@gmail.com

TurboTeam 集先鋒
that's (g)it.
By Ming-der Wang
that's (g)it.
- 1,964