Upload
lucien-lee
View
4.514
Download
6
Embed Size (px)
DESCRIPTION
Cloud Computing-Based Services Design and Programming 2014S Git Tutorial For the Beginner
初心者 Git 上手攻略Cloud Computing-Based Services Design and Programming
Lucien Lee
First, let Me Tell you a Story…
2
work alone
3
不知不覺分裂增⽣生的報告
4
work with others
5
BY Dropbox, right?
那些年,⼀一起撞牆的夥伴
6
惱羞成怒
Does it sounds happened to you before?
8
All You NeeD is __________
9
Version Control
什麼是版本控制
• 儲存進度
• 讀取進度
• 管理不同記錄
極簡篇
什麼是版本控制• 我們想要:
• 知道為什麼變更 • 知道什麼時候變更 • 知道變更了什麼 • 知道是誰變更的 • 切換到過去的進度 • 切換不同的版本 • 合併不同的版本
史詩篇
「不怕神一樣的對手,只怕豬一樣的隊友」
12
What to fear are not the God-like opponents but the goose-like teammates.
git: Distributed Version Control Systems
13
‣ 安裝 ‣ 設定 ‣ 初始化
1. SET UP YOUR Git
安裝
Windows Mac
Homebrewbrew install githttp://msysgit.github.io/
git for windows
SourceTree
私⼼心建議
https://help.github.com/articles/set-up-git
GUI
私⼼心建議
github app
$ git config --global user.name <名字>$ git config --global user.email <email>$ git config --global color.ui true
17
設定身分
// under your project $ git init
18
初始化
幫你在專案裡生成 git 所需檔案: .git
‣基礎概念 ‣儲存流程 ‣.gitignore
basic Git in Local
基礎概念
20
s tIworking directory staging area repository你放在資料夾裡的檔案 暫存你想存檔的檔案們 檔案的前世今⽣生
藏在 .git 裡status 會告訴你你本來就看得到的資料夾結構
commit 提交到儲存庫,你可以想像成存檔
$ git statusOn branch masternothing to commit, working directory clean
21
檢視 git 的當前狀態
儲存流程
22
s tIworking directory staging area repository
stage files
commit把想儲存的紀錄加到暫存區裡
建⽴立⼀一個存檔記錄
// From working directory to staging area$ git add <檔案>!
// From staging area to working directory$ git reset HEAD <檔案>
23
加到暫存區
// commit to repo$ git commit <檔案>$ git commit -m “commit message”!
// get commit from repo to staging area$ git reset ——soft HEAD^
24
送進檔案庫
Take a snapshot
‧Commit 會在目前的狀態,建立一個新的記錄點,之後可以回復到過去的紀錄點。
25
$ git log
26
git log
檢視過去提交的歷史紀錄,看看過去有哪些存檔
基礎概念-檔案狀態
27
s tIworking directory staging area repository
untracked
unmodified
modified
staged
add filescommit
edit files
stage files
#Untracked files$ git add <檔案>#Changes not staged for commit$ git add <檔案>
28
git Add
git add 可以用於加入追蹤新檔案,也可用於加入新修改的檔案到暫存區。
$ git diff
29
git diff
讓你知道哪些檔案被修改,並且比較修改了什麼內容
$ git rm <檔案>
30
git rm
刪除檔案,同時從 repo 的歷史紀錄移除
.gitignore
• 有些檔案永遠不想 add
• 編輯器的暫存檔
• C/C++ 的 binary 檔、物件檔
• 把你不想要被 add 的檔案,寫進 .gitignore
• 可以使⽤用 *,表⽰示全部檔案
• ⽤用正規表⽰示式的語法
$ git reset ——hard
32
情境⼩小幫⼿手不⼩小⼼心把 code 弄壞了,我想全部重來,那要怎麼回到上⼀一個存檔點的樣⼦子?
清除所有與最近一次 commit 不同的修改
$ git checkout —— <檔案>
33
情境⼩小幫⼿手只有⼀一個檔案,想改回之前的狀態,要怎麼做?
將修改過的檔案回復到最近一次 commit 的狀態
‣遠端概念 ‣設定連線 ‣Push and Pull
git with remote repository
基礎概念
35
s tIworking directory staging area repositoryytremote repository
基礎概念
36
s tIworking directory staging area repositoryytremote repositorys tIworking directory staging area repository
ssh 連線 public key 認證
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
SSH: Secure Shell
38
yt1. 交換公鑰
o2. ⽤用公鑰加密要傳的訊息
3. 各⾃自⽤用私鑰解密 收到的訊息
每台電腦會有: 公鑰(public key)-⽤用於加密 私鑰(private key)-⽤用於解密
set up ssh key with gitgub
39
Reference: https://help.github.com/articles/generating-ssh-keys
$ 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
$ 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/ 目錄底下
#mac$ pbcopy < ~/.ssh/id_rsa.pub #windows$ clip < ~/.ssh/id_rsa.pub
42
Step 3: Add your SSH key to GitHub
複製你的公鑰的剪貼簿,然後你再交給 Gitbub
Step 3: Add your SSH key to GitHub
43
設定連線
44
t yt連結遠端 repo現有 repo
t yt下載遠端 repo
remote repo
remote repo建⽴立repo
1.
2.
$ git remote add origin git@HOSTPATH
45
連結遠端 repo
設定你的本地 repo 跟遠端連結
在 github 建⽴立新 repo
46
$ git clone git@HOSTPATH:directory name
47
複製遠端 repo
從遠端複製一份 repo 回來,在當前的資料夾建 立新資料夾 <directory name>,並自動連結。
$ git remote -v
48
git remote
檢視你遠端 repo 的資訊
$ git remote add [shortname] [url]
49
git remote add
origin 是我們遠端 repo 常取的慣例名字
push and pull
50
t ytpush: 將本地版本推上遠端
pull: 從遠端拉回本地並且合併到本地端
$ git push origin master
51
git push
本地端預設(分支)叫做 master,我們把本地 master 推送到遠端
$ git pull origin master
52
git pull
‣分支概念 ‣切換分支 ‣合併分支 ‣與他人合作
branch
Branch
54
• 建⽴立⼀一條新的歷史紀錄
• 多種版本獨⽴立開發
• 分⼯工合作時,幫助⼤大家不會互相衝突。
• 最終可以合併在⼀一起
$ git branch <new branch>
55
git Branch
從當前分支上,新增一條名為 <new branch> 的分支
What branch look like
• ⽣生成新 branch 時,並不會切換到新 branch。
56
#list local branch$ git branch#list local and remote branch$ git branch -a #delete the branch$ git branch -D <branch>
57
Manipulate branch
$ git checkout <branch>
58
change your branch
$ git checkout -b <branch>
59
New and change branch simultaneously
What branch look like
60
Detached Head
• checkout 可以幫助你回到到過去的 commit 版本
• 此時⾃自⼰己跟任何⼀一個 branch 都不⼀一樣
• Git 幫你暫時建⽴立的 branch
$ 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
merge
63
merge• 當兩個⼈人分別開發在不同分⽀支時,最終會合併為完整版。
• 改動不同檔案、同⼀一檔案不同區域
• auto merge • 改到同⼀一⾏行,GG。
• conflict • ⼈人⼯工處理
64
$ git merge <branch>
65
合併分⽀支
把另外一支 <branch> 合併到自己身上來。
當發⽣生 conflict
• ⾸首先 git status ,了解狀況。
• 衝突的檔案會標⽰示兩個版本的資訊,⼿手動合併存檔
• git add 改好的檔案
• git commit
$ 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
<<<<<<< HEAD<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at s[email protected]</div>>>>>>>> bugFix
68
衝突的地⽅方會標⽰示出來
$ git commit -am “Merge bugFix”
69
最後⼿手動 commit
$ git reset --hard
70
情境⼩小幫⼿手Merge 時發⽣生 conflict,想取消 merge
將修改過的檔案回復到最近一次 commit 的狀態
$ git checkout ——ours <file>$ git checkout ——theirs <file>
71
情境⼩小幫⼿手Merge 時發⽣生 conflict,想把 conflict 的地⽅方,直接⽤用某⽀支 branch 的
ours 表示衝突部分使用目前分支的,theirs 則反過來。
多⼈人協作
72
git 合作⽅方式 • 有⼀一⽀支 branch 放完整可以動的程式,⽐比如說 master 或是 dev。
• 開發功能時,⼤大家從主線分⽀支出來,獨⽴立開發。
• 開發完後,合併回去主線。
• ⾃自⼰己的分⽀支⾃自⼰己合。
• 或是 git fetch origin ,把所有遠端分⽀支搬回家,再⾃自⼰己合併。
極簡篇
git flow
• http://nvie.com/posts/a-successful-git-branching-model/
75https://guides.github.com/introduction/flow/index.html
‣自動補齊 ‣Git 命令別名 ‣stash 暫存操作
appendix
#macbrew install git bash-completion#Ubuntu/Debiansudo apt-get install git-core bash-completion
77
⾃自動補⿑齊
在Bash shell 底下,自動幫你補完打到一半的 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 縮寫,幫助自己指令打少一點
暫時儲存區 stash
• 不想提交當前完成了⼀一半的程式,但是卻不得不修改⼀一個緊急 Bug
• 把⺫⽬目前⼯工作狀況丟到暫存區,這時候你的⼯工作區和上次剛提交內容的狀況是⼀一樣,所以你可以放⼼心的修 Bug。
• 修完可以再把東⻄西叫回來。
• 有點像 stack 的暫存區。
#將⺫⽬目前所做的修改都暫存起來$ git stash#取出最新⼀一次的暫存$ git stash apply#取出最新⼀一次的暫存並將他從暫存清單中移除$ git stash pop#清除所有暫存$ git stash clear
80
git stash
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