90
[ chusiang @nsysucm ~ ] $ cat .profile # Author: 凍仁翔 / chusiang.lai (at) gmail.com # Blog: http://note.drx.tw # Modified: 2015 - 04 - 29 17:50 GitHub & Bacis with SourceTree

Git and Github basic with SourceTree

Embed Size (px)

Citation preview

Page 1: Git and Github basic with SourceTree

[ chusiang@nsysucm ~ ] $ cat .profile# Author: 凍仁翔 / chusiang.lai (at) gmail.com# Blog: http://note.drx.tw# Modified: 2015-04-29 17:50

GitHub&Bacis with SourceTree

Page 2: Git and Github basic with SourceTree

關於凍仁翔

• 5 年以上的 Git 使⽤用經驗。

• 前 Foxconn軟體⼯工程師。

• 現職 DrSays IT ⼯工程師。

• 相關作品和著作:

• LifeZMD

• @be_vimmer_tw

• 《完全⽤用 GNU/Linux ⼯工作》

• 凍仁的筆記

2

Page 3: Git and Github basic with SourceTree

準備好⼀一探究竟嗎?開始囉!3

Page 4: Git and Github basic with SourceTree

OutlineI. Git 是什麼?

II. GitHub ⼜又是什麼?

III. Git & GitHub 可以怎麼玩?

IV. 10 招成為 Git ⼤大師。

V. GitHub 隨堂測驗。

4

Page 5: Git and Github basic with SourceTree

Ⅰ. Git 是什麼?

5

Page 6: Git and Github basic with SourceTree

–Wikipedia

Git是⼀一個分散式版本控制軟體,最初由林納斯.托⽡瓦茲 (Linus Torvalds)創作,於 2005 年以 GPL釋出。最初是為了更有效率的管理 Linux Kernel 開

發⽽而設計的。

6

Page 7: Git and Github basic with SourceTree

Git 是什麼?• SCM (Source Code Management, Software

Configuration Management)

• VCS (Version Control Software)

• 分散式的版本控制系統

• ⾃自由軟體 (GPL License)

7

⽏毋需 Server,可離線使⽤用

不怕被襲斷、不怕成為軟體孤兒

Page 8: Git and Github basic with SourceTree

沒有版本控管的世界⼤大家都經歷過的「⼿手殘」年代

8

Page 9: Git and Github basic with SourceTree

9

檔案夾版本控制

網路芳鄰版本控制

• FTP 版本控制• 壓縮檔版本控制• Google 雲端硬碟版本控制• Dropbox 版本控制• ...

Page 10: Git and Github basic with SourceTree

那位⺩王OX到底是在寫什麼啊!10

Page 11: Git and Github basic with SourceTree

有 SVN 的世界⼤大家都經歷過的「⼿手殘」年代 #2

11

Page 12: Git and Github basic with SourceTree

沒有網路是要怎麼寫 code 啊!!12

Page 13: Git and Github basic with SourceTree

有 Git 的世界⼿手殘? 爛 code? 傻傻分的清楚

13

Page 14: Git and Github basic with SourceTree

誰? 在何時?⾃自我敘述

做了什麼?

在 Git 的世界裡您可以看到…

詳細資訊

14

Page 15: Git and Github basic with SourceTree

凡⾛走過必留痕跡

15

Page 16: Git and Github basic with SourceTree

–jserv

「Git 是⼀一個可以讓現在的⾃自⼰己、過去的⾃自⼰己和未來的⾃自⼰己共同開發的版本控制系統。」

16

Page 17: Git and Github basic with SourceTree

Git 10 週年,Linux 之⽗父談他是怎麼在 10 天內開發完成

伯樂在線 - http://goo.gl/zvavk3

17

Page 18: Git and Github basic with SourceTree

Ⅱ. GitHub ⼜又是什麼?

18

Page 19: Git and Github basic with SourceTree

–chusiang

GitHub 是⼀一個提供友善的 Git 代管、Issue tracking ... 等的程式碼社交平台。

19

Page 20: Git and Github basic with SourceTree

GitHub 官⽅方網站https://github.com/

20

Page 21: Git and Github basic with SourceTree

Git Web

21

Page 22: Git and Github basic with SourceTree

git diff

22

Page 23: Git and Github basic with SourceTree

Issue Tracking

23

Page 24: Git and Github basic with SourceTree

Wiki

24

Page 25: Git and Github basic with SourceTree

Follwers

25

Page 26: Git and Github basic with SourceTree

Ⅲ. Git 和 GitHub 可以怎麼玩?

26

Page 27: Git and Github basic with SourceTree

多⼈人協同開發• LifeZMD 為 Charlie, Chu, Dick,

Olga, Steven, chusiang 於 iDSBG 第

三屆 HTML5 Hackathon 所製作。

• ⽐比賽成績:第 4 名。

• ⽐比賽時間:2 天 1 夜。

• 團隊⼈人數:6 ⼈人。

LifeZMD - http://goo.gl/OHJK9u27

Page 28: Git and Github basic with SourceTree

靜態網站SM 是個使⽤用 GitHub Pages 服務和的

HTML, CSS 和 Javascript 所寫成的⼤大

字報Web Apps。

SM - http://sm.drx.tw

28

Page 29: Git and Github basic with SourceTree

送 Patch覺得別⼈人的專案不夠完美?

那就先 fork 別⼈人的專案,

改完 code 再送 Pull Requests 唄!

@be_vimmer_tw - http://goo.gl/TVYHsL29

Page 30: Git and Github basic with SourceTree

寫書• 藉由 GitHub 和 GitBook 服務的整

合,我們可以使⽤用 Markdown 語法撰

寫⽂文章,並整理成冊。

• GitBook 同時⽀支援Web, PDF, ePub

和 mobi 等多種格式。

完全⽤用 GNU/Linux ⼯工作 - http://goo.gl/OHJK9u30

Page 31: Git and Github basic with SourceTree

持續整合 (CI)

搭配強⼤大的 Jenkins CI,即可進⾏行

⾃自動化測試 (Web UI, Unit test)、

編譯 (AOSP)、通知訊息 (Email,

HipChat, Slack) ... 等整合。

Jenkins CI - https://ci.jenkins-ci.org/31

Page 32: Git and Github basic with SourceTree

–chusiang

「有了 Git 就好⽐比寫程式可以跟玩單機遊戲⼀一樣,可以隨時儲存和讀取進度,不管是要快速破關還是

要體驗各個分⽀支劇情都可以輕鬆的辦到。」

32

Page 33: Git and Github basic with SourceTree

Ⅳ. 10 招成為 Git ⼤大師

33

Page 34: Git and Github basic with SourceTree

在開始之前須準備

功預善其事,必先利其器

的Tool34

Page 35: Git and Github basic with SourceTree

SourceTree Terminalor

35

Page 36: Git and Github basic with SourceTree

還有跑跳江湖

的 ID 和Email36

Page 37: Git and Github basic with SourceTree

# 設定使⽤用者名稱$ git config --global user.name "foo"

# 設定電⼦子信箱$ git config --global user.email "[email protected]"

37

Page 38: Git and Github basic with SourceTree

第 1 招:複製 (clone) 與初始化 (init)想快速上⼿手?那就先從抄開始唄!

38

Page 39: Git and Github basic with SourceTree

再 clone 前,請先 fork為便於同步本以下操作環境,請先⾄至 GitHub 進⾏行 fork。

左鍵

39

Page 40: Git and Github basic with SourceTree

複製 (clone)若不是從⾃自⼰己的 GitHub 進⾏行複製 (clone),預設是沒有權限上傳 (push) 的喔!

40

Page 41: Git and Github basic with SourceTree

左鍵左鍵

41

# 複製 (clone)$ git clone [email protected]:chusiang/sandbox.git

紅字部份請改成⾃自⼰己的帳號紅字部份請改成⾃自⼰己的帳號紅字部份請改成⾃自⼰己的帳號

Page 42: Git and Github basic with SourceTree

初始化 (init)在建⽴立⼀一個新的 Git 專案之前,我們需⼿手動建⽴立本地端的數據庫 (Local Repository),但若有前⼈人 (通常會是系統管理員) 建⽴立好,或 fork 其它專案,這時我們就可以略過。

42

Page 43: Git and Github basic with SourceTree

左鍵

左鍵

# 建⽴立專案⺫⽬目錄並切換⾄至該⺫⽬目錄底下$ mkdir /Users/jonny/vcs/demo; cd $_

# 建⽴立本地數據庫 (local repository)$ git init 43

Page 44: Git and Github basic with SourceTree

第 2 招:新增 (add)、移動 (mv)、刪除 (rm)可想成是 CRUD 裡的 Create, Upgrade, Delete。

44

Page 45: Git and Github basic with SourceTree

右鍵

# 新增 (Add)、刪除 (Remove)$ git { add | rm } README.md

# 移動 (Move)$ git mv README.md foo.md

Page 46: Git and Github basic with SourceTree

第 3 招:提交 (commit)在 Git 的每⼀一動裡,都是⽤用 commit 來記錄的。

46

Page 47: Git and Github basic with SourceTree

# 提交 (Commit)$ git commit README.md

git commit 左鍵

輸⼊入⽂文字

47

Page 48: Git and Github basic with SourceTree

Upgradeto Lv1

48

Page 49: Git and Github basic with SourceTree

第 4 招:查看狀態 (status) 與歷史記錄 (log)在 Git 的世界迷路時,⽤用 status 和 log 就對了。

49

Page 50: Git and Github basic with SourceTree

查看狀態 (status)現在做到哪⼀一動了? 看 status 就知道了!

50

Page 51: Git and Github basic with SourceTree

# 版本狀態 (Status)$ git status

git status

51

Page 52: Git and Github basic with SourceTree

查看歷史記錄 (log)在 Console 裡凍仁習慣⽤用 git lg 的別名 (alias) 來看版本樹。

52

Page 53: Git and Github basic with SourceTree

# 觀看版本記錄$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

git log

53

Page 54: Git and Github basic with SourceTree

第 5 招:查看記錄 (checkout)講完了儲存進度,接下來就是讀取 (Read) 進度。

54

Page 55: Git and Github basic with SourceTree

# 查看某進度時的記錄$ git checkout 09a3d66

左鍵

左鍵

左鍵

55

Page 56: Git and Github basic with SourceTree

第 6 招:⽐比對版本差異 (diff)剛剛改了什麼 code? 有 diff 即可⼀一⺫⽬目瞭然。

56

Page 57: Git and Github basic with SourceTree

# ⽐比對 (Different) 當前版本差異$ git diff HEAD

57

Page 58: Git and Github basic with SourceTree

Upgradeto Lv2

58

Page 59: Git and Github basic with SourceTree

第 7 招:推送 (push)上傳⾄至遠端 (remote) 的數據庫 (Repository)。

59

Page 60: Git and Github basic with SourceTree

# 推送 (Push)$ git push origin master

左鍵

左鍵

60

Page 61: Git and Github basic with SourceTree

第 8 招:拉取 (pull)從遠端 (remote) 數據庫 (Repository) 下載。

61

Page 62: Git and Github basic with SourceTree

# 拉取 (Pull)$ git pull origin master

左鍵

左鍵

62

Page 63: Git and Github basic with SourceTree

第 9 招:捨棄修改 (checkout)程式寫爛了? 那就重頭來過吧!

63

Page 64: Git and Github basic with SourceTree

# 藉由 checkout 捨棄修改$ git checkout README.md

左鍵

左鍵

64

Page 65: Git and Github basic with SourceTree

65

Upgradeto Lv3

Page 66: Git and Github basic with SourceTree

第⼗十招:分⽀支 (branch) 與合併 (merge)學會 branch 和 merge 即可說是習得⼤大成功⼒力了!

66

Page 67: Git and Github basic with SourceTree

版本樹在開始前先看⼀一下結果。

master

dev

67

Page 68: Git and Github basic with SourceTree

分⽀支 (branch)同⼀一款遊戲,總有不同的分⽀支劇情。

68

Page 69: Git and Github basic with SourceTree

# 建⽴立分⽀支$ git branch dev$ git checkout -b dev

左鍵

master69

左鍵

Page 70: Git and Github basic with SourceTree

# 列出分⽀支$ git branch

master70

Page 71: Git and Github basic with SourceTree

# 藉由 checkout 切換⾄至 dev 分⽀支$ git checkout dev

左鍵 x2

dev

master71

Page 72: Git and Github basic with SourceTree

# 於 dev 分⽀支新增檔案,並提交$ git add branch_demo.txt$ git commit -m 'Demo for branch'dev

72

Page 73: Git and Github basic with SourceTree

Branch? 舉⼿手可得開了新的 branch 後還要在新的 branch 裡 commit 過才看得到分⽀支樹。

dev

master

Uncommitted changes

73

Page 74: Git and Github basic with SourceTree

合併 (merge)劇情分⽀支久了,總有回到主線的⼀一天。

74

Page 75: Git and Github basic with SourceTree

左鍵 x2

# 藉由 checkout 切換⾄至 master 分⽀支$ git checkout masterdev

master

75

Page 76: Git and Github basic with SourceTree

# 於 master 合併 dev 分⽀支$ git merge --no-ff dev

master76

Page 77: Git and Github basic with SourceTree

合併完成!

master77

Page 78: Git and Github basic with SourceTree

您已習得上乘⼼心法Congratulation !

78

Page 79: Git and Github basic with SourceTree

您已習得上乘⼼心法Congratulation !

79

Page 80: Git and Github basic with SourceTree

Ⅴ. GitHub 隨堂測驗

80

Page 81: Git and Github basic with SourceTree

基本操作 (Basic)1. 登⼊入 Github。

- https://github.com/

2. 對 Lab 的 Repository 進⾏行 fork。- https://github.com/chusiang/sandbox/

3. 下載程式碼。$ git clone https://github.com/YourName/sandbox.git

4. 新增檔案並提交。$ git { add | status | commit }

5. 上傳程式碼。$ git push

81

Login GitHub

fork Repository

git clone

git add

git commit

git status

git push

Finish?Yes

No

Page 82: Git and Github basic with SourceTree

進階操作 (Advanced)1. 於其它路徑再次下載程式碼。

$ git clone https://github.com/YourName/sandbox.git

2. 新增、修改、刪除檔案並提交多次。$ git { add | mv | rm | status | commit }

3. 新增分⽀支並提交多次。

4. 切換⾄至各個紀錄點和分⽀支。$ git checkout OXOXOXX

5. 合併分⽀支。

6. 上傳程式碼。$ git push origin master

82

dev

master

Finish

Page 83: Git and Github basic with SourceTree

⼯工商服務

83

Page 84: Git and Github basic with SourceTree

2015

2015.10.?

- http://mopcon.org

84

Page 85: Git and Github basic with SourceTree

官⽅方網站http://mopcon.org

粉絲群http://fb.me/mopcon

85

Page 86: Git and Github basic with SourceTree

86

Page 87: Git and Github basic with SourceTree

http://coscup.org87

Page 88: Git and Github basic with SourceTree

Q & A

嚴禁拍打餵⻝⾷食

請參考現場共筆⽂文件 - http://goo.gl/aqTTYO

88

Page 89: Git and Github basic with SourceTree

Reference• 現場共筆⽂文件 - http://goo.gl/aqTTYO

• 現場直播網址 - https://youtu.be/5ftNjrpzVqM

• 連猴⼦子都能懂的 Git ⼊入⾨門指南 | ⾙貝格樂 (Backlog) - http://backlogtool.com/git-guide/tw/

• 寫給⼤大家的 Git 教學 | slideshare - http://www.slideshare.net/littlebtc/git-5528339

• Git 實務圖解 | slideshare - http://www.slideshare.net/pokaichang72/git-42427674

• Git 官⽅方⽂文件 - http://git-scm.com/book/zh-tw/v1

• 圖⽚片來源:

• 國家地理雜誌 - http://www.ngtaiwan.com

• 10 Years of Git | Atlassian Git Tutorial - https://www.atlassian.com/git/articles/10-years-of-git/

89

Page 90: Git and Github basic with SourceTree

E N D90