81
初心者 Git 上手攻略 Cloud Computing-Based Services Design and Programming Lucien Lee

初心者 Git 上手攻略

Embed Size (px)

DESCRIPTION

Cloud Computing-Based Services Design and Programming 2014S Git Tutorial For the Beginner

Citation preview

Page 1: 初心者 Git 上手攻略

初心者 Git 上手攻略Cloud Computing-Based Services Design and Programming

Lucien Lee

Page 2: 初心者 Git 上手攻略

First, let Me Tell you a Story…

2

Page 3: 初心者 Git 上手攻略

work alone

3

Page 4: 初心者 Git 上手攻略

不知不覺分裂增⽣生的報告

4

Page 5: 初心者 Git 上手攻略

work with others

5

BY Dropbox, right?

Page 6: 初心者 Git 上手攻略

那些年,⼀一起撞牆的夥伴

6

Page 7: 初心者 Git 上手攻略

惱羞成怒

Page 8: 初心者 Git 上手攻略

Does it sounds happened to you before?

8

Page 9: 初心者 Git 上手攻略

All You NeeD is __________

9

Version Control

Page 10: 初心者 Git 上手攻略

什麼是版本控制

• 儲存進度

• 讀取進度

• 管理不同記錄

極簡篇

Page 11: 初心者 Git 上手攻略

什麼是版本控制• 我們想要:

• 知道為什麼變更 • 知道什麼時候變更 • 知道變更了什麼 • 知道是誰變更的 • 切換到過去的進度 • 切換不同的版本 • 合併不同的版本

史詩篇

Page 12: 初心者 Git 上手攻略

「不怕神一樣的對手,只怕豬一樣的隊友」

12

What to fear are not the God-like opponents but the goose-like teammates.

Page 13: 初心者 Git 上手攻略

git: Distributed Version Control Systems

13

Page 14: 初心者 Git 上手攻略
Page 15: 初心者 Git 上手攻略

‣ 安裝 ‣ 設定 ‣ 初始化

1. SET UP YOUR Git

Page 16: 初心者 Git 上手攻略

安裝

Windows Mac

Homebrewbrew install githttp://msysgit.github.io/

git for windows

SourceTree

私⼼心建議

https://help.github.com/articles/set-up-git

GUI

私⼼心建議

github app

Page 17: 初心者 Git 上手攻略

$ git config --global user.name <名字>$ git config --global user.email <email>$ git config --global color.ui true

17

設定身分

Page 18: 初心者 Git 上手攻略

// under your project $ git init

18

初始化

幫你在專案裡生成 git 所需檔案: .git

Page 19: 初心者 Git 上手攻略

‣基礎概念 ‣儲存流程 ‣.gitignore

basic Git in Local

Page 20: 初心者 Git 上手攻略

基礎概念

20

s tIworking directory staging area repository你放在資料夾裡的檔案 暫存你想存檔的檔案們 檔案的前世今⽣生

藏在 .git 裡status 會告訴你你本來就看得到的資料夾結構

commit 提交到儲存庫,你可以想像成存檔

Page 21: 初心者 Git 上手攻略

$ git statusOn branch masternothing to commit, working directory clean

21

檢視 git 的當前狀態

Page 22: 初心者 Git 上手攻略

儲存流程

22

s tIworking directory staging area repository

stage files

commit把想儲存的紀錄加到暫存區裡

建⽴立⼀一個存檔記錄

Page 23: 初心者 Git 上手攻略

// From working directory to staging area$ git add <檔案>!

// From staging area to working directory$ git reset HEAD <檔案>

23

加到暫存區

Page 24: 初心者 Git 上手攻略

// commit to repo$ git commit <檔案>$ git commit -m “commit message”!

// get commit from repo to staging area$ git reset ——soft HEAD^

24

送進檔案庫

Page 25: 初心者 Git 上手攻略

Take a snapshot

‧Commit 會在目前的狀態,建立一個新的記錄點,之後可以回復到過去的紀錄點。

25

Page 26: 初心者 Git 上手攻略

$ git log

26

git log

檢視過去提交的歷史紀錄,看看過去有哪些存檔

Page 27: 初心者 Git 上手攻略

基礎概念-檔案狀態

27

s tIworking directory staging area repository

untracked

unmodified

modified

staged

add filescommit

edit files

stage files

Page 28: 初心者 Git 上手攻略

#Untracked files$ git add <檔案>#Changes not staged for commit$ git add <檔案>

28

git Add

git add 可以用於加入追蹤新檔案,也可用於加入新修改的檔案到暫存區。

Page 29: 初心者 Git 上手攻略

$ git diff

29

git diff

讓你知道哪些檔案被修改,並且比較修改了什麼內容

Page 30: 初心者 Git 上手攻略

$ git rm <檔案>

30

git rm

刪除檔案,同時從 repo 的歷史紀錄移除

Page 31: 初心者 Git 上手攻略

.gitignore

• 有些檔案永遠不想 add

• 編輯器的暫存檔

• C/C++ 的 binary 檔、物件檔

• 把你不想要被 add 的檔案,寫進 .gitignore

• 可以使⽤用 *,表⽰示全部檔案

• ⽤用正規表⽰示式的語法

Page 32: 初心者 Git 上手攻略

$ git reset ——hard

32

情境⼩小幫⼿手不⼩小⼼心把 code 弄壞了,我想全部重來,那要怎麼回到上⼀一個存檔點的樣⼦子?

清除所有與最近一次 commit 不同的修改

Page 33: 初心者 Git 上手攻略

$ git checkout —— <檔案>

33

情境⼩小幫⼿手只有⼀一個檔案,想改回之前的狀態,要怎麼做?

將修改過的檔案回復到最近一次 commit 的狀態

Page 34: 初心者 Git 上手攻略

‣遠端概念 ‣設定連線 ‣Push and Pull

git with remote repository

Page 35: 初心者 Git 上手攻略

基礎概念

35

s tIworking directory staging area repositoryytremote repository

Page 36: 初心者 Git 上手攻略

基礎概念

36

s tIworking directory staging area repositoryytremote repositorys tIworking directory staging area repository

ssh 連線 public key 認證

Page 37: 初心者 Git 上手攻略

remote Repo service

• Cooperate with others, private used or open source

• Issue tracking, Wiki feature

• Over 5 people in a private repo will charge

• In this course, we use github and open source

Page 38: 初心者 Git 上手攻略

SSH: Secure Shell

38

yt1. 交換公鑰

o2. ⽤用公鑰加密要傳的訊息

3. 各⾃自⽤用私鑰解密 收到的訊息

每台電腦會有: 公鑰(public key)-⽤用於加密 私鑰(private key)-⽤用於解密

Page 39: 初心者 Git 上手攻略

set up ssh key with gitgub

39

Reference: https://help.github.com/articles/generating-ssh-keys

Page 40: 初心者 Git 上手攻略

$ cd ~/.ssh$ ls -al# Lists the files in your .ssh directory

40

Step 1: Check for SSH keys

檢查你有沒有 id_rsa.pub or id_dsa.pub

Page 41: 初心者 Git 上手攻略

$ ssh-keygen -t rsa -C "[email protected]"# Creates a new ssh key, using the provided email as a label

41

Step 2: Generate a new SSH key

生成新的 ssh key,在你的 ~/.ssh/ 目錄底下

Page 42: 初心者 Git 上手攻略

#mac$ pbcopy < ~/.ssh/id_rsa.pub #windows$ clip < ~/.ssh/id_rsa.pub

42

Step 3: Add your SSH key to GitHub

複製你的公鑰的剪貼簿,然後你再交給 Gitbub

Page 43: 初心者 Git 上手攻略

Step 3: Add your SSH key to GitHub

43

Page 44: 初心者 Git 上手攻略

設定連線

44

t yt連結遠端 repo現有 repo

t yt下載遠端 repo

remote repo

remote repo建⽴立repo

1.

2.

Page 45: 初心者 Git 上手攻略

$ git remote add origin git@HOSTPATH

45

連結遠端 repo

設定你的本地 repo 跟遠端連結

Page 46: 初心者 Git 上手攻略

在 github 建⽴立新 repo

46

Page 47: 初心者 Git 上手攻略

$ git clone git@HOSTPATH:directory name

47

複製遠端 repo

從遠端複製一份 repo 回來,在當前的資料夾建 立新資料夾 <directory name>,並自動連結。

Page 48: 初心者 Git 上手攻略

$ git remote -v

48

git remote

檢視你遠端 repo 的資訊

Page 49: 初心者 Git 上手攻略

$ git remote add [shortname] [url]

49

git remote add

origin 是我們遠端 repo 常取的慣例名字

Page 50: 初心者 Git 上手攻略

push and pull

50

t ytpush: 將本地版本推上遠端

pull: 從遠端拉回本地並且合併到本地端

Page 51: 初心者 Git 上手攻略

$ git push origin master

51

git push

本地端預設(分支)叫做 master,我們把本地 master 推送到遠端

Page 52: 初心者 Git 上手攻略

$ git pull origin master

52

git pull

Page 53: 初心者 Git 上手攻略

‣分支概念 ‣切換分支 ‣合併分支 ‣與他人合作

branch

Page 54: 初心者 Git 上手攻略

Branch

54

• 建⽴立⼀一條新的歷史紀錄

• 多種版本獨⽴立開發

• 分⼯工合作時,幫助⼤大家不會互相衝突。

• 最終可以合併在⼀一起

Page 55: 初心者 Git 上手攻略

$ git branch <new branch>

55

git Branch

從當前分支上,新增一條名為 <new branch> 的分支

Page 56: 初心者 Git 上手攻略

What branch look like

• ⽣生成新 branch 時,並不會切換到新 branch。

56

Page 57: 初心者 Git 上手攻略

#list local branch$ git branch#list local and remote branch$ git branch -a #delete the branch$ git branch -D <branch>

57

Manipulate branch

Page 58: 初心者 Git 上手攻略

$ git checkout <branch>

58

change your branch

Page 59: 初心者 Git 上手攻略

$ git checkout -b <branch>

59

New and change branch simultaneously

Page 60: 初心者 Git 上手攻略

What branch look like

60

Page 61: 初心者 Git 上手攻略

Detached Head

• checkout 可以幫助你回到到過去的 commit 版本

• 此時⾃自⼰己跟任何⼀一個 branch 都不⼀一樣

• Git 幫你暫時建⽴立的 branch

Page 62: 初心者 Git 上手攻略

$ git logcommit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7

Author: Scott Chacon <[email protected]>

Date: Sat Mar 15 16:40:33 2008 -0700

! removed unnecessary test code

!commit a11bef06a3f659402fe7563abf99ad00de2209e6

Author: Scott Chacon <[email protected]>

Date: Sat Mar 15 10:31:28 2008 -0700

! first commit

$ git checkout a11bef062

checkout to past commit

Page 63: 初心者 Git 上手攻略

merge

63

Page 64: 初心者 Git 上手攻略

merge• 當兩個⼈人分別開發在不同分⽀支時,最終會合併為完整版。

• 改動不同檔案、同⼀一檔案不同區域

• auto merge • 改到同⼀一⾏行,GG。

• conflict • ⼈人⼯工處理

64

Page 65: 初心者 Git 上手攻略

$ git merge <branch>

65

合併分⽀支

把另外一支 <branch> 合併到自己身上來。

Page 66: 初心者 Git 上手攻略

當發⽣生 conflict

• ⾸首先 git status ,了解狀況。

• 衝突的檔案會標⽰示兩個版本的資訊,⼿手動合併存檔

• git add 改好的檔案

• git commit

Page 67: 初心者 Git 上手攻略

$ git statusOn branch masterYou have unmerged paths. (fix conflicts and run "git commit")!

Unmerged paths: (use "git add <file>..." to mark resolution)!

both modified: index.html

67

conflict

Page 68: 初心者 Git 上手攻略

<<<<<<< HEAD<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at [email protected]</div>>>>>>>> bugFix

68

衝突的地⽅方會標⽰示出來

Page 69: 初心者 Git 上手攻略

$ git commit -am “Merge bugFix”

69

最後⼿手動 commit

Page 70: 初心者 Git 上手攻略

$ git reset --hard

70

情境⼩小幫⼿手Merge 時發⽣生 conflict,想取消 merge

將修改過的檔案回復到最近一次 commit 的狀態

Page 71: 初心者 Git 上手攻略

$ git checkout ——ours <file>$ git checkout ——theirs <file>

71

情境⼩小幫⼿手Merge 時發⽣生 conflict,想把 conflict 的地⽅方,直接⽤用某⽀支 branch 的

ours 表示衝突部分使用目前分支的,theirs 則反過來。

Page 72: 初心者 Git 上手攻略

多⼈人協作

72

Page 73: 初心者 Git 上手攻略

git 合作⽅方式 • 有⼀一⽀支 branch 放完整可以動的程式,⽐比如說 master 或是 dev。

• 開發功能時,⼤大家從主線分⽀支出來,獨⽴立開發。

• 開發完後,合併回去主線。

• ⾃自⼰己的分⽀支⾃自⼰己合。

• 或是 git fetch origin ,把所有遠端分⽀支搬回家,再⾃自⼰己合併。

極簡篇

Page 74: 初心者 Git 上手攻略

git flow

• http://nvie.com/posts/a-successful-git-branching-model/

Page 75: 初心者 Git 上手攻略

75https://guides.github.com/introduction/flow/index.html

Page 76: 初心者 Git 上手攻略

‣自動補齊 ‣Git 命令別名 ‣stash 暫存操作

appendix

Page 77: 初心者 Git 上手攻略

#macbrew install git bash-completion#Ubuntu/Debiansudo apt-get install git-core bash-completion

77

⾃自動補⿑齊

在Bash shell 底下,自動幫你補完打到一半的 git 指令

Page 78: 初心者 Git 上手攻略

$ git config --global alias.co checkout$ git config --global alias.br branch$ git config --global alias.ci commit$ git config --global alias.st status

78

命令別名

設定一些 git 縮寫,幫助自己指令打少一點

Page 79: 初心者 Git 上手攻略

暫時儲存區 stash

• 不想提交當前完成了⼀一半的程式,但是卻不得不修改⼀一個緊急 Bug

• 把⺫⽬目前⼯工作狀況丟到暫存區,這時候你的⼯工作區和上次剛提交內容的狀況是⼀一樣,所以你可以放⼼心的修 Bug。

• 修完可以再把東⻄西叫回來。

• 有點像 stack 的暫存區。

Page 80: 初心者 Git 上手攻略

#將⺫⽬目前所做的修改都暫存起來$ git stash#取出最新⼀一次的暫存$ git stash apply#取出最新⼀一次的暫存並將他從暫存清單中移除$ git stash pop#清除所有暫存$ git stash clear

80

git stash

Page 81: 初心者 Git 上手攻略

CopyRight

• 仙劍奇俠傳

• https://octodex.github.com/

• http://pcottle.github.io/learnGitBranching/

• http://www.wei-wang.com/ExplainGitWithD3/

• https://speakerdeck.com/mrorz/git-and-heroku-tutorial-at-ccsp-2012f