git 101 課程

講師: 王銘德

mingderwang@gmail.com

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 為例 >

Git branching model

< Git 的另一個好處是 "開 branches 不用錢" >

* local 你可以盡量開 branches

典型版空模式

Git 的分散式集中

local

local

local

local

remote

注意: 每個人會有一份 local copy

而且可能不樣的 branches

我們稱這 origin 為 "remote"

  • 通常只有一個 origin remote
  • 但我們也可以有多個 remote (平常比較少這樣用)
  • 因此我們每個人的 local 也未必跟別人相同
  • 其他細節, 跳到 http://nvie.com/posts/a-successful-git-branching-model/ 來說明

開始學 git 指令

(按   往下按鍵)

常用 Git 指令 - 15 個就夠了

$ 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 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 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

// 回到原來狀態

That's (g)it

git 就是這麼一回事

+ 不斷得練習

 

(請按 右鍵)

補充資料

解除 CONFLIT (衝突)

<<<<<<< HEAD
      - "5140:5140/udp"
    expose:
      - "5140/udp"
=======
      - "5011:5011"
      - "5140:5140/udp"
      - "5013:5013"
    expose:
      - "5011"
      - "5140/udp"
      - "5013"
>>>>>>> 1f3b6a13b19d5723589d5ba47676194fda7b8fca

挑你要的部分, 其他行刪掉

整塊不要, 刪掉

(只能綠或紅,二選一)

刪掉

Q&A

mingderwang@gmail.com

TurboTeam 集先鋒

git 101

By Ming-der Wang

git 101

  • 1,929