SlideShare a Scribd company logo
Git
2018/10/19 @ NTUOSC
關於我
有點懶得寫 Zzzzz
為什麼這份簡報這麼醜?
想像你現在在和組員一起做通識報告...
醒醒吧,你的組員不會做的。
「安安,我們來寫一份報告吧」
「我剛剛改了這邊喔」
「等等,我改了一樣的地方」
「我覺得之前那個版本比較好,有人有備份嗎 QQ」
「乾,這垃圾誰寫的」
「啊啊啊啊,我筆電掉到馬桶裡了檔案都不見了怎麼辦」
「現在停修來得及嗎 TAT」
為了避免剛剛那些鳥事...
● 通識報告.docx
● 通識報告更新版.docx
● 通識報告更新 2.docx
● 通識報告 20181019.docx
● 通識報告 20181019 有註解版.docx
● 通識報告最終版.docx
● 通識報告最終版修訂.docx
● 通識報告最終一定不會再改的版.docx
● 通識報告之我幹嘛修這門課啊版.docx
● 通識報告 dklndyuikmnmsdv.docx ← 怕撞到其他檔名
Hmm 太低能了? 不如建立一個 Excel 來記錄吧!
https://www.ptt.cc/bbs/Soft_Job/
M.1524799027.A.895.html
你需要的是版本控制 QQ
懶得找更多圖了 Zzzzz
集中式版本控制系統 vs 分散式版本控制系統
Git (Local)
Git
● 不要唸成 Jit
● 反正就是一個分散式版本控制系統啦
● Linus Torvalds 寫的
那些你需要先知道的事情
1. Git 就是 Git,不是 GitHub,也不是 SVN 進化版,更不是綠豆糕
-- 宋晶宜 沒說過這句話
2. Git 多數操作都不用網路 :)
3. 就像斯斯一樣,Git 中檔案的狀態大概可以分三種:
已提交 (committed)、已修改 (modified) 、已暫存 (staged)
好啦,其實斯斯有五種
4. 在一個空資料夾中,必須要先 git init 後才可以開始使用 Git
先來弄點 Config
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git config --global color.ui true
git config --global core.editor vim
(這樣就退不出去了,狂賀)
Git 入門與應用
git add
1. 把檔案從 working directory 加入 staging area
2. 再加入 staging area 之前稱為 untracked
3. 加入 staging area 後稱為 staged
4. 可以用 git status 看目前檔案狀態
git commit
● 把檔案從 Staging Area 送到 Git Repo
● Commit 時會建立一個 Snapshot
● 可以加上一個附註來解釋這個 Commit 在做啥(commit log)
● Commit 實際上一串英文數字混和的東西(其實是 SHA1)
675b205c49cd26787084f100e27ba0bfd3b00c52
● .gitkeep
Practice!
● git init
● git add
● git commit
git log / git lg
● 可以用 git log 看目前 commit 了什麼
● 啊不過 git log 好醜 QQ
git config --global alias.lg "log --color --graph
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s
%Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"
● 上面是貼辛酸的,請從這裡複製:
https://gist.github.com/johanmeiring/3002458
復原
● 將檔案從 staging area 移出(Untrack)
git rm --cached filename
● 取消某個 commit(檔案不變,commit 本身消失)
git reset <commit_id>
● 還原到某個 commit 點(commit 後的變更會消失,commit 本身消失)
git reset <commit_id> --hard
復原
● 還原到某 commit 點(commit 本身還在,僅檔案暫時回到過去)
git checkout <commit_id>
● 取消 commit(會使用一個新的 commit 來取消前一個 commit)
git revert <commit_id>
Practice!
● git log / git lg
● git reset
● git checkout
● git revert
分支
回到剛剛的通識報告...
「怒,AMD 一定會重返農藥」
「哪有,森 77,NVIDIA 才是王道」
「絕交喔」
分支
圖片來源
git branch Feature
 HEAD 
分支
圖片來源
 HEAD 
分支
圖片來源
 HEAD 
git checkout Master
話說天下大勢,合久必分,分久必合 …
「欸我們不要再吵了,我快被當了,趕快把兩份報告合併吧」
「恩 也是 QQ」
git merge
git merge Feature
 HEAD 
git rebase
git rebase Master
列出 Branch
● git branch
● git branch -v
● git branch --merged
● git branch --no-merged
Practice!
嘗試作以下事件:
● 從 master 分支出一個 branch 叫做 feature
● 隨便修改 feature 上的幾個檔案並做幾次 commit
● 切回 master 並嘗試把 feature 給 merge 回來
● git lg
● 你發現了什麼?
Fast Forword
● Fast Forword:如果 feature 是基於 master 的延伸,merge 時 master 還沒有離
開「基準點」,則會直接把 master 的指針移動到 feature 所在的位置,不會創造
新的 commit
● 不想要 Fast Forward 怎麼辦 OAO
○ git merge feature --no-ff
聽起來很完美對吧 … 並沒有。
「我剛剛改了李白的介紹喔」
「喔我也改了那裡欸,怎麼辦」
「我寫的比較好,用我的吧」
「明明就我的比較好」
「乾,決鬥!」
Conflict
剛好改到同個地方,Git 不知道怎麼 Merge / Rebase 時,就會出現 Conflict
Git 入門與應用
打開 Unmerged 的檔案尋找 Conflict 點
<<<<<<< HEAD
change on master branch, oh no conflict
=======
changes on branch a
>>>>>>> a
修完後 git add 再 git commit 即可完成 merge
Practice!
自己創造一個 Conflict 然後解決他吧!
Git (Local + Server)
GitHub
先來簡單說一下 GitHub 怎麼用 …
● 建立帳號(不想講)
● 建立 Repo
● 建立 SSH Key 並新增到 GitHub
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
圖片來源
origin
● 遠端伺服器上的 branch
● master -> origin/master
feature -> origin/feature
● 在 local 端 origin/ 預設隱藏,需要用 git branch -a 召喚出他們
當 origin 不同於 local 時....
origin 落後於 local 時:
● git push 把 commit 推到 server 上
origin 超越 local 時:
● git pull ( = git fetch + git merge )
● 如果 local 有東西還沒 push,可能需要 merge
○ git pull 時他會幫你做好 :)
● 因為 merge 會創造無意義的 commit,所以也可以用 rebase 的方式
git pull --rebase
其他需要知道的 Git Command
git diff
git diff <commit_id> :比較該 commit 與 working directory 的差異
git diff <older_comment> <newer_commit>:比較兩個 commit 的差異
喔對,GitHub 上的 diff 很好用也很漂亮喔
git blame
恩,名稱已經說明一切了。
「這段垃圾 Code 到底是誰寫的!」「喔等等,是我寫的欸。」
git tag
● 為 commit 上一個 tag
● 通常用於標示 release 點
● Lightweight Tag: git tag <tag_name> <commit_id>
● Annotated Tag: git tag -a <tag_name> <commit_id>
Git Workflow
A successful Git branching model
簡報就做到這,我要去趕早九的課了,掰

More Related Content

PDF
Git 可以做到的事
PDF
Code review on github training ( beginner )
PDF
Code review on github training ( intermediate )
PDF
你畢業後要任職的軟體業到底都在做些什麼事
PDF
Non-MVC Web Framework
PDF
超酷炫科幻 UI:QML 入門
PDF
Using vim
PPTX
GIT實務操作與理論
Git 可以做到的事
Code review on github training ( beginner )
Code review on github training ( intermediate )
你畢業後要任職的軟體業到底都在做些什麼事
Non-MVC Web Framework
超酷炫科幻 UI:QML 入門
Using vim
GIT實務操作與理論

Similar to Git 入門與應用 (9)

PDF
A successful git branching model 導讀
PPTX
大家應該都要會的工具 Git 從放棄到會用2-分支篇
PPTX
Git and git hub
PDF
寫給大家的 Git 教學
PPTX
Git入門介紹
PPTX
Git流派與工作流程.pptx
PPTX
電子內容管理 使用Git 與 github 1
PDF
Noder eyes for frontend guys
PPTX
大家應該都要會的工具 Git 從放棄到會用1-基礎篇
A successful git branching model 導讀
大家應該都要會的工具 Git 從放棄到會用2-分支篇
Git and git hub
寫給大家的 Git 教學
Git入門介紹
Git流派與工作流程.pptx
電子內容管理 使用Git 與 github 1
Noder eyes for frontend guys
大家應該都要會的工具 Git 從放棄到會用1-基礎篇
Ad

More from Allen Chou (9)

PDF
黑箱演算法的獨裁時代
PDF
瀏覽器大戰 @ NTUOSC
PDF
資料庫入門 Part1
PDF
資料庫入門 Part2
PDF
資料庫入門 Part3
PDF
應用密碼學入門 - HITCON CMT 2018
PDF
淺談電子投票的技術與困境
PDF
Basic Introduction to Global Surveillance
PDF
Got Your PW - 一場入門資安的微旅行
黑箱演算法的獨裁時代
瀏覽器大戰 @ NTUOSC
資料庫入門 Part1
資料庫入門 Part2
資料庫入門 Part3
應用密碼學入門 - HITCON CMT 2018
淺談電子投票的技術與困境
Basic Introduction to Global Surveillance
Got Your PW - 一場入門資安的微旅行
Ad

Git 入門與應用