SlideShare a Scribd company logo
Git in a nutshell
Git 是什麼?
  What is Git?
Git 是什麼

git
[git]

n.

飯桶﹐無用的人
版本控制系統

™  多人協同開發
 ™  每個人開發一個小功能


™  改錯了還有救
 ™  可回溯到之前版本
 ™  可知道是誰改爛的
源起

™  Linus Torvalds
    (Linux 的作者)

™  為了管理 Linux Kernel
    程式碼而開發
採用 Git 的專案

™  Android                    ™  jQuery

™  Linux Kernel               ™  Perl

™  Debian, Fedora, openSUSE   ™  Qt

™  Git                        ™  Ruby on Rails

™  Gnome                      ™  VLC

™  GIMP                       ™  Wine

™  GTK+                       ™  X.org Server

                               ™  x264
Git 的好處
       Why Git is better than X
http://zh-tw.whygitisbetterthanx.com/
優於其他管理系統
本機管理系統
本機管理系統
中央管理系統 (CVCS)
中央管理系統 (CVCS)
分散管理系統 (DVCS)
分散管理系統 (DVCS)
沒有網路也能工作

™  Git once, coding anywhere!

™  Branch 開不用錢

™  看 log 超快
適合超大量資料

™  操作速度超快

™  儲存的檔案很小

™  連 Linux Kernel 都 hold 得住
Branch 不用錢

™  開 branch 幾乎零成本

™  開 branch 改東西,不怕改爛主軸

™  切換 branch 很簡單

™  Git 的 merge 很聰明
Git 的運作概觀
   Git overview
關於版本




The Others’ Way
關於版本




The Git’s Way
Git in a nutshell
Git in a nutshell
Git 是這樣看待 commit 的
Git 是這樣看待 commit 的
一些用語

™  HEAD
   ™  指出目前位於哪個節點

™  origin
   ™  預設的原始 repo 名稱

™  master
   ™  預設的 branch 名稱

™  bare repo
   ™  分享的 repo,習慣以 ProjName.git 命名
支援協定

™  file://
    ™  本機檔案,例如用 Dropbox 架 repo

™  ssh://
    ™  安全,適合寫入

™  git://
    ™  效率好,但沒有認證機制,適合唯讀

™  http:// or https://
    ™  簡單,效率低,可突破防火牆
忽略規則

™  空目錄會被忽略
  ™  習慣放個 .gitkeep 在裡頭避免忽略

™  用 .gitignore 排除不需要的檔案
  ™    https://github.com/github/gitignore
  ™    放在 Working tree 的根目錄
  ™    記得也要 commit
  ™    能自動產生的東西都該排除
  ™    全域設定放在 ~/.gitignore
Git 常用指令
  Git commands
git config

™  使用者資訊,commit 時會用到
  ™  git config --global user.name “Your Name”
  ™  git config --global user.email “account@mail.address”
git config

™  讓終端機顯示上色
 ™  git config --global color.ui true
git config

™  For Linux & Mac
   ™  git config --global core.autocrlf input
   ™  git config --global core.safecrlf true

™  For Windows
   ™  git config --global core.autocrlf true
   ™  git config --global core.safecrlf true
git init

™  初始化 repo

™  建立 bare repo
   ™  git init --bare
git add

™  將新檔案加入追蹤

™  將修改過的檔案加入 stage

™  git add .
   ™  將所有檔案加入追蹤
   ™  常接在 git init 之後
git commit

™  提交 stage 裡的資料

™  git commit -m “Commit message”
git clone

™  複製一份 repo

™  從 bare repo 複製
   ™  git clone protocol://path/to/bare_repo.git

™  從現有 repo 建立 bare repo
   ™  git clone --bare <dir_name> <bare_dir_name>.git
git status

™  檢視目前狀態
git branch

™  列出或建立 branch

™  git branch <branch_name>

™  branch name 強烈建議用 [0-9A-Za-z_-.]
git tag

™  列出或建立 tag

™  tag name 強烈建議用 [0-9A-Za-z_-.]
git tag (cont.)

™  建立 tag
  ™  git tag <tag_name>
  ™  git push --tags

™  移除 tag
  ™  git tag -d <tag_name>
  ™  git push origin :refs/tags/<tag_name>
git checkout

™  切換到其他 branch

™  git checkout -b <branch_name> =
    git branch <branch_name> +
    git checkout <branch_name>
git stash

™  保存目前的工作環境

™  git stash apply --index <stash_name>
   ™  將保存的環境倒回來,包含 stage 的內容
git reset

™  重置 stage 的狀態

™  git reset HEAD <file_name>
   ™  重置單一檔案

™  git reset HEAD
   ™  重置所有檔案

™  git reset --hard HEAD
   ™  連 working tree 也重置
git revert

™  取消上個 commit

™  git revert HEAD
   ™  會建立一個新 commit
git fetch

™  將本機 repo 與遠端 repo 同步
git pull

™  將本機 repo 與遠端 repo 同步,並更新 working tree

™  git pull = git fetch + git merge
git push

™  推送本機 repo 資料到遠端 repo
git merge

™  合併資料到當前 branch
Git 工作流程
  Work with Git
工作流程
ref. http://progit.org/book/ch5-1.html
Centralized Workflow
       中央控管
      適合小型團隊
Integration-Manager Workflow
          整合式管理員
    適合網路多人協同專案,例如 GitHub.com
Dictator and Lieutenants Workflow
            司令官與副手
        適合超大專案,例如 Linux Kernel
分支模型
ref. http://nvie.com/posts/a-successful-git-branching-model/
Git in a nutshell
master

™  一直存在的分支

™  記錄每個上線的版本

™  只有在推出新版時才會變動

™  所謂的穩定版(stable version)
develop

™  一直存在的分支

™  必須總是 build 得過

™  new feature 跟 enhancement 都在這裡

™  所謂的測試版(beta version)
feature

™  從 develop 分支出來

™  要 merge 回 develop

™  新增或修改功能時才會出現

™  功能完成就砍掉
release

™  從 develop 分支出來
™  要 merge 回 master,並且上 tag
™  要 merge 回 develop
™  管理者決定要 release 新版時才會建立
™  release 之後就砍掉
™  修改版本號、Release Note、ReadMe
™  最後測試並修正測試發現的 bug
hotfix

™  從 master 分支出來

™  要 merge 回 master,並且上 tag

™  要 merge 回 develop 或 release

™  緊急修正問題專用

™  修正之後就砍掉

™  修改版本號、Release Note、ReadMe
總結

™  不同 branch 有不同用途,不得混用

™  任何更動都要開 branch

™  除了 master 跟 develop 之外,完成任務就砍掉 branch
建議工作規範
  My proposal
時常同步

™  每天或每週一次

™  有備份才安心

™  習慣 Git 的操作

™  降低 merge 困難度
愛用 branch

™  開 branch 不用錢,所以任何變動都開 branch

™  不要在 master 或 develop 修改程式
詳細 commit

™  記得移除行尾空白

™  一次只 commit 一個主題

™  commit message 寫清楚
  ™  第一行:主旨,少於 50 字元
  ™  第二行:空白
  ™  第三行:詳情,每行少於 74 字元
關於檔案

™  避免不同平台產生亂碼
 ™  檔名只能用 ASCII 字元	
 ™  檔名不得使用可能非法字元,如 / ? < > *  : | ”
 ™  文字檔編碼一律用 UTF-8


™  避免不同軟體產生亂碼
 ™  文字檔內容一律用 ASCII 字元(即:不得用中文)
Git 工具
Git on Windows & Mac
安裝 Git

™  Mac
   ™  http://code.google.com/p/git-osx-installer/



™  Windows
   ™  http://code.google.com/p/msysgit/
跨平台工具
工程師的浪漫
Mac
SourceTree (Git/Hg)
http://itunes.apple.com/us/app/sourcetree-git-hg/id411678673?mt=12
GitX (L)
http://gitx.laullon.com/
Windows
TortoiseGit
http://code.google.com/p/tortoisegit/
SmartGit
 http://www.syntevo.com/smartgit/index.html
Cross-platform, free for non-commercial purpose.
EGit for Eclipse
  http://eclipse.org/egit/
Git 參考資料
  Git references
™  簡介
  ™  http://git-scm.com/
  ™  http://speakerdeck.com/u/dannvix/p/20111214-git-in-
      a-nutshell
  ™  http://www.slideshare.net/littlebtc/git-5528339



™  教學
  ™  http://progit.org/
  ™  http://marklodato.github.com/visual-git-guide/index-
      en.html
  ™  http://ihower.tw/blog/archives/category/git
™  其他
  ™  http://nvie.com/posts/a-successful-git-branching-
      model/
  ™  https://github.com/github/gitignore
  ™  http://zh-tw.whygitisbetterthanx.com/
  ™  http://people.debian.org.tw/~chihchun/2008/12/19/
      linus-torvalds-on-git/
你問,我…盡量答
   Q&A

More Related Content

PDF
Yet another introduction to Git - from the bottom up
PDF
Git Tutorial 教學
PPTX
Git基礎介紹
PDF
Git tutorial for windows user (給 Windows user 的 Git 教學)
PDF
版本控制 使用Git & git hub
PDF
Introduction to git
PPTX
工程師必備第一工具 - Git
PDF
Git and Github basic with SourceTree
Yet another introduction to Git - from the bottom up
Git Tutorial 教學
Git基礎介紹
Git tutorial for windows user (給 Windows user 的 Git 教學)
版本控制 使用Git & git hub
Introduction to git
工程師必備第一工具 - Git
Git and Github basic with SourceTree

What's hot (20)

PDF
幸福快樂的完美結局
PPTX
Git and git hub
PPTX
Mercurial簡介與教學
PDF
Git 版本控制 (使用教學)
PDF
連哈秋都懂的Git教學
PPTX
Git 入門與實作
PDF
Git由超淺入超深
PDF
初心者 Git 上手攻略
PDF
Git 版本控制系統 -- 從微觀到宏觀
PDF
寫給大家的 Git 教學
PPTX
Visual Studio 2015 與 Git 開發實戰
PDF
A successful git branching model 導讀
PPTX
Git & Sourcetree 介紹
PDF
Git 經驗分享
PPTX
Git flow 與團隊合作
PPTX
Gitlab
PDF
Git與source tree 基礎教學
PPTX
git merge 與 rebase 的觀念與實務應用
PDF
Git 入门实战
ODP
Git 程式碼版本控制軟體介紹
幸福快樂的完美結局
Git and git hub
Mercurial簡介與教學
Git 版本控制 (使用教學)
連哈秋都懂的Git教學
Git 入門與實作
Git由超淺入超深
初心者 Git 上手攻略
Git 版本控制系統 -- 從微觀到宏觀
寫給大家的 Git 教學
Visual Studio 2015 與 Git 開發實戰
A successful git branching model 導讀
Git & Sourcetree 介紹
Git 經驗分享
Git flow 與團隊合作
Gitlab
Git與source tree 基礎教學
git merge 與 rebase 的觀念與實務應用
Git 入门实战
Git 程式碼版本控制軟體介紹
Ad

Viewers also liked (20)

PDF
Getting Git Right
PDF
Introduction to git
PDF
Getting Git
PDF
Git 101: Git and GitHub for Beginners
PDF
Git in a nutshell
PDF
Git 101 Presentation
PDF
Git and Github
KEY
Git and GitHub
PDF
Advanced Git
PPT
Git 101 - Crash Course in Version Control using Git
PPTX
Introduction to Git/Github - A beginner's guide
PDF
Quick Introduction to git
PPTX
Introduction to git
PDF
PPTX
Sys05 uso consapevole di git - beyond the basic
PDF
Introduzione a GIT - Laboratorio di Web Design 2014/15
PDF
GITT (part 1 of 2)
PDF
KEY
Perchè Git?
PDF
Linux Day 2015 Genova
Getting Git Right
Introduction to git
Getting Git
Git 101: Git and GitHub for Beginners
Git in a nutshell
Git 101 Presentation
Git and Github
Git and GitHub
Advanced Git
Git 101 - Crash Course in Version Control using Git
Introduction to Git/Github - A beginner's guide
Quick Introduction to git
Introduction to git
Sys05 uso consapevole di git - beyond the basic
Introduzione a GIT - Laboratorio di Web Design 2014/15
GITT (part 1 of 2)
Perchè Git?
Linux Day 2015 Genova
Ad

Similar to Git in a nutshell (20)

PDF
Git Tutorial
PPTX
Git Essence Tutorial
PDF
First meetingwithgit
PPTX
Git使用入门
PPT
Git 超簡單學習懶人包(軟體程式版本控管系統)
ODP
Git 教學
PDF
Git+使用教程
PPTX
Git &amp; git hub v1.2
PPT
Git flow
PDF
Git 簡介(古時候的簡報備份)
PPTX
Git 使用介绍
PPT
Git share
PPTX
Git内部培训文档
PPTX
Git & git flow
PPTX
Git原理与实战 201607
PDF
Build Your Own Android Toolchain from scratch
PDF
Learning to Use Git | WeiYuan
PDF
PPTX
20150313 ian git
PPTX
20170510 git 懶人包
Git Tutorial
Git Essence Tutorial
First meetingwithgit
Git使用入门
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 教學
Git+使用教程
Git &amp; git hub v1.2
Git flow
Git 簡介(古時候的簡報備份)
Git 使用介绍
Git share
Git内部培训文档
Git & git flow
Git原理与实战 201607
Build Your Own Android Toolchain from scratch
Learning to Use Git | WeiYuan
20150313 ian git
20170510 git 懶人包

Git in a nutshell