Git 入門 
Mudream 
台大資訊2014前瞻營
當我們有一個程式可能要維護一段時間... 
1. 打開 
2. 修改 
3. 存檔
09/01 
fix something 
09/05 09/07 09/10
當我們想要保留每天的進度.... 
當我們希望知道每天的差異.... 
或者,我們的版本長這樣?!
Maria: fix bug#8 
Aya: change slide’s title 
Masio: change bmp folder 
我們需要知道 
1. 誰(WHO) 
2. 甚麼時候(WHEN) 
3. 為甚麼(WHY) 
做了這個改變 
Aya: fix indent
Git操作簡介
本機的版本管理 
首先,先cd 到想要管理的資料夾(示範: MyFirstGitTest) 
然後打git init 
mudream@HP: ~/MyFirstGitTest$ git init 
Initialized empty Git repository in /home/mudream/MyFirstGitTest/.git/
本機的版本管理 
我們可以先試著寫一個檔案: 
mudream@HP: ~/MyFirstGitTest$ echo “hello git” > test1.in 
mudream@HP: ~/MyFirstGitTest$ more test1.in 
hello git
本機的版本管理 
雖然資料夾底下有test1.in,可是,容器裡面並沒有在觀察 
這檔案。我們要把它加進觀察。 
mudream@HP: ~/MyFirstGitTest$ git add test1.in
本機的版本管理 
它到底有沒有被加進去呢? git status!! 
mudream@HP: ~/MyFirstGitTest$ git status 
#On branch master 
# 
#Initial commit 
# 
#Changes to be commited: 
# (use “git rm --cached <file>...” to unstage) 
# 
# new file: test1.in 
#
本機的版本管理 
好,最後一步,送交!! 
mudream@HP: ~/MyFirstGitTest$ git commit -m “my first commit” 
1 file changed, 1 insertions(+) 
create mode 100644 test1.in
本機的版本管理 
現在來看一下目前容器裡面的log吧! 
mudream@HP: ~/MyFirstGitTest$ git log
本機的版本管理 
來回顧一下,剛剛到底做了哪些動作 
1. git init ----- 初始化一個容器 
2. git add <filename> -----告訴git,待會兒commit時要對他 
拍照,此時被add的file,會被紀錄在暫存區。 
3. git status ----- 看目前容器狀態 
4. git commit -----把準備要拍照的檔案們,成為一個新的版 
本
圖解 
一開始的版本: 
test.c 
觀察清單: 
EMPTY 
最初 
版本
圖解 
git add test.c 
觀察清單: 
test.c 
最初 
版本
圖解 
git commit 
最初 
版本 
觀察清單: 
EMPTY 
新版 
本
一些概念 
1. 假如每個版本都是複製一份,不是會佔很大的空間? 
所以git不是這樣實作 
2. HEAD是神馬? 
可以當作當前操作版本的指標
分岔
branch? 
或許我們有時會希望在某些版本上作一個標記,方便日後切 
換。 
1. 怎麼標記 
2. 怎麼切換
branch? 
像說我們的版本是這樣 
C1 C2 
HEAD 
我們要怎麼切回C2或 
C1?
branch? : git log 
我們要先查看一些訊息:git log 
mudream@HP:~/testgit$ git log 
…. 
commit 2d94ba78f43fc78aad6c1e308e41bcbc2be7166b 
Author: Mudream <mudream@mu.com> 
Date: Mon Sep 22 23:14:54 2014 +0800 
commit 2 
commit 95047fa7170e729983a078c5161ddfdcbd5ea775 
Author: Mudream <mudream@mu.com> 
Date: Mon Sep 22 23:14:45 2014 +0800 
commit 1
branch? : git checkout 
每個版本都會有個獨一無二的神秘字串 
mudream@HP:~/testgit$ git checkout 2d94 
Note: checking out '2d94'. 
You are in 'detached HEAD' state. You can look around, make 
experimental changes and commit them, and you can discard any 
commits you make in this state without impacting any branches by 
performing another checkout. 
If you want to create a new branch to retain commits you create, you 
may do so (now or later) by using -b with the checkout command again. 
Example: 
git checkout -b new_branch_name 
HEAD is now at 2d94ba7... commit 2
branch? 
從訊息我們可以知道我們成功切到C2了!! 
HEAD 
C1 C2
branch? 
假如我們這時作個commit? 
C1 C2 
HEAD 
CC
branch? : git checkout master 
要怎麼切回原本的阿?? 
git checkout master 
C1 C2 
HEAD 
等等....為神麼是master CC
branch? : branch name 
其實是這樣的,樹枝還有名稱 
所以checkout 可以直接指定樹枝名稱 
C1 C2 
HEAD 
CC 
master
branch? : branch name 
那可不可以幫一些其他版本取名? 
當然可以,我們切到CC 
C1 C2 
HEAD 
CC 
master
branch? : branch name 
git branch mynewbranch 
C1 C2 
HEAD 
CC 
master 
mynewbranch
merge: merge a branch 
git checkout master 
git merge mynewbranch 
C1 C2 C3 
HEAD 
mynewbranch 
CC 
master 
M1
Github, Bitbucket….
來看看其他人分享的Git Repository 
1. Github github.com 
2. Bitbucket
可以把其他人的容器複製下來喔 
以下以/ntuparallellab/judgegirl 示範 
https://github.com/ntuparallellab/judgegirl
複製這個網址
可以把其他人的容器複製下來喔 
mudream@HP: ~$ git clone <複製的網址> 
… 
mudream@HP: ~$ cd ntuparallellab/ 
mudream@HP: ~/ntuparallellab$ 
耶! 可以看code惹~~
神奇的操作
一些神奇的操作 
假如不小心add 到一些不該add 的檔案怎辦QQ? 
例如說想要取消對file.c的關注 
mudream@HP: ~/MyFirstGitTest$ git status 
#On branch master 
# 
#Initial commit 
# 
#Changes to be commited: 
# (use “git rm --cached <file>...” to unstage) 
# 
# new file: file.c 
#
一些神奇的操作 
reset head: 
mudream@HP: ~/MyFirstGitTest$ git reset HEAD file.c
一些神奇的操作 
假如想要取消剛剛某個檔案的操作... 
例如file.c 
mudream@HP: ~/MyFirstGitTest$ git status 
#On branch master 
#Changed but not updated: 
# (use “git add <file>...” to update what will be committed) 
# 
# modified: file.c 
#
一些神奇的操作 
checkout file 
mudream@HP: ~/MyFirstGitTest$ git checkout -- file.c 
注意!! 這操作非常危險,這檔案的更 
改都會消失,除非你真的不需要此時 
的修改,否則不要下這指令。
Reference 
1. git 官方網站 
2. git 版本控制 
3. 圖解git 
4. Learn Git Branching

Git入門介紹