SlideShare une entreprise Scribd logo
1  sur  81
Télécharger pour lire hors ligne
初心者 Git 上手攻略
Cloud Computing-Based Services Design and Programming
Lucien Lee
First,letMeTellyouaStory…
2
work alone
3
不知不覺分裂增⽣生的報告
4
workwithothers
5
BYDropbox,right?
那些年,⼀一起撞牆的夥伴
6
惱羞成怒
Does it sounds
happened to you before?
8
AllYouNeeDis__________
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
Homebrew
brew 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 status	
On branch master	
nothing 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 files
commit
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 repository ytremote repository
基礎概念
36
s tIworking directory staging area repository
ytremote repository
s 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
yt
1. 交換公鑰
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 "your_email@example.com"	
# 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 yt
push: 將本地版本推上遠端
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 log	
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7	
Author: Scott Chacon <schacon@gee-mail.com>	
Date: Sat Mar 15 16:40:33 2008 -0700	
!
removed unnecessary test code	
!
commit a11bef06a3f659402fe7563abf99ad00de2209e6	
Author: Scott Chacon <schacon@gee-mail.com>	
Date: Sat Mar 15 10:31:28 2008 -0700	
!
first commit	
$ git checkout a11bef0
62
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 status	
On branch master	
You 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.support@github.com</div>	
=======	
<div id="footer">	
please contact us at
support@github.com	
</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/
75
https://guides.github.com/introduction/flow/index.html
‣自動補齊
‣Git 命令別名
‣stash 暫存操作
appendix
#mac	
brew install git bash-completion	
#Ubuntu/Debian	
sudo 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

Contenu connexe

Tendances

バージョン管理のワークフロー
バージョン管理のワークフローバージョン管理のワークフロー
バージョン管理のワークフロー
add20
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
민태 김
 

Tendances (20)

バージョン管理のワークフロー
バージョン管理のワークフローバージョン管理のワークフロー
バージョン管理のワークフロー
 
Git 版本控制 (使用教學)
Git 版本控制 (使用教學)Git 版本控制 (使用教學)
Git 版本控制 (使用教學)
 
Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰
 
Git flowの活用事例
Git flowの活用事例Git flowの活用事例
Git flowの活用事例
 
Gitはじめの一歩
Gitはじめの一歩Gitはじめの一歩
Gitはじめの一歩
 
一人でもはじめるGitでバージョン管理
一人でもはじめるGitでバージョン管理一人でもはじめるGitでバージョン管理
一人でもはじめるGitでバージョン管理
 
いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0いつやるの?Git入門 v1.1.0
いつやるの?Git入門 v1.1.0
 
いつやるの?Git入門
いつやるの?Git入門いつやるの?Git入門
いつやるの?Git入門
 
デザイナのためのGit入門
デザイナのためのGit入門デザイナのためのGit入門
デザイナのためのGit入門
 
15分でわかるGit入門
15分でわかるGit入門15分でわかるGit入門
15分でわかるGit入門
 
ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理ノンプログラマでも今日から使える「Git」でバージョン管理
ノンプログラマでも今日から使える「Git」でバージョン管理
 
Git and Github
Git and GithubGit and Github
Git and Github
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with Git
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
はじめようGit
はじめようGitはじめようGit
はじめようGit
 
社内Git勉強会向け資料
社内Git勉強会向け資料社内Git勉強会向け資料
社内Git勉強会向け資料
 
はじめてのGit forデザイナー&コーダー
はじめてのGit forデザイナー&コーダーはじめてのGit forデザイナー&コーダー
はじめてのGit forデザイナー&コーダー
 
Git One Day Training Notes
Git One Day Training NotesGit One Day Training Notes
Git One Day Training Notes
 
Introduction to Git and Github
Introduction to Git and GithubIntroduction to Git and Github
Introduction to Git and Github
 

Similaire à 初心者 Git 上手攻略

First meetingwithgit
First meetingwithgitFirst meetingwithgit
First meetingwithgit
Rhythm Sun
 
Mercurial簡介與教學
Mercurial簡介與教學Mercurial簡介與教學
Mercurial簡介與教學
芳本 林
 

Similaire à 初心者 Git 上手攻略 (20)

Git and Github basic with SourceTree
Git and Github basic with SourceTreeGit and Github basic with SourceTree
Git and Github basic with SourceTree
 
Learning to Use Git | WeiYuan
Learning to Use Git | WeiYuanLearning to Use Git | WeiYuan
Learning to Use Git | WeiYuan
 
Github简介及实用入门
Github简介及实用入门Github简介及实用入门
Github简介及实用入门
 
Github簡介
Github簡介Github簡介
Github簡介
 
Git introduction
Git introductionGit introduction
Git introduction
 
Gitlab
GitlabGitlab
Gitlab
 
COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報
 
Git 使用介绍
Git 使用介绍Git 使用介绍
Git 使用介绍
 
First meetingwithgit
First meetingwithgitFirst meetingwithgit
First meetingwithgit
 
Learn git
Learn gitLearn git
Learn git
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战
 
Mercurial簡介與教學
Mercurial簡介與教學Mercurial簡介與教學
Mercurial簡介與教學
 
Git & git flow
Git & git flowGit & git flow
Git & git flow
 
Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)
 
Git&Github Tutorial
Git&Github TutorialGit&Github Tutorial
Git&Github Tutorial
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 
20160420 - git intro
20160420 - git intro20160420 - git intro
20160420 - git intro
 
Git 入門與實作
Git 入門與實作Git 入門與實作
Git 入門與實作
 
Git基础培训
Git基础培训Git基础培训
Git基础培训
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 

Plus de Lucien Lee

Plus de Lucien Lee (9)

[JSDC 2021] Blockchain 101 for Frontend Engs
[JSDC 2021] Blockchain 101 for Frontend Engs[JSDC 2021] Blockchain 101 for Frontend Engs
[JSDC 2021] Blockchain 101 for Frontend Engs
 
【真的假的 Cofacts】事實查核案例分享
【真的假的 Cofacts】事實查核案例分享【真的假的 Cofacts】事實查核案例分享
【真的假的 Cofacts】事實查核案例分享
 
Start Prototyping From Keynote
Start Prototyping From KeynoteStart Prototyping From Keynote
Start Prototyping From Keynote
 
[HackCampus] 圖書館自習區體驗設計-行政及閱覽組回饋篇
[HackCampus] 圖書館自習區體驗設計-行政及閱覽組回饋篇[HackCampus] 圖書館自習區體驗設計-行政及閱覽組回饋篇
[HackCampus] 圖書館自習區體驗設計-行政及閱覽組回饋篇
 
[HackCampus] 圖書館自習區體驗設計-探索篇
[HackCampus] 圖書館自習區體驗設計-探索篇[HackCampus] 圖書館自習區體驗設計-探索篇
[HackCampus] 圖書館自習區體驗設計-探索篇
 
Hack the hacking
Hack the hackingHack the hacking
Hack the hacking
 
Multidisciplinary @NTUIM
Multidisciplinary @NTUIMMultidisciplinary @NTUIM
Multidisciplinary @NTUIM
 
智慧家庭音樂體驗設計工作坊
智慧家庭音樂體驗設計工作坊智慧家庭音樂體驗設計工作坊
智慧家庭音樂體驗設計工作坊
 
CSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & CompassCSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & Compass
 

初心者 Git 上手攻略