狠狠撸

狠狠撸Share a Scribd company logo
Git tutorial for windows user


                cloudtu
      http://cloudtu.blogspot.tw
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               2
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               3
版控系统发展史




                               4
          * 取自「Git Tutorial」
版控系统发展史




                           5
          * 取自「Git Tutorial」
版控系统发展史




          * 取自「Git Tutorial」
                       6
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               7
骋颈迟设计目标




      * 取自「Git in a nutshell」   8
骋颈迟设计目标




                        9
     * 取自「寫給大家的Git教學」
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               10
安裝Git
? 安裝流程
 – Step1.安裝msysgit
    ? Git-1.7.11-preview20120710.exe
        – http://msysgit.googlecode.com/files/Git-1.7.11-
          preview20120710.exe
 – Step2.安裝tortoisegit
    ? TortoiseGit 1.7.12.0(32-bit)
        – http://tortoisegit.googlecode.com/files/TortoiseGit-1.7.12.0-
          32bit.msi
    ? TortoiseGit 1.7.12.0(64-bit)
        – http://tortoisegit.googlecode.com/files/TortoiseGit-1.7.12.0-
          64bit.msi
? 補充說明
 – TortoiseGit官方網址
    ? http://code.google.com/p/tortoisegit/
                                                                          11
安裝Git
? 安裝msysgit




          只勾選這二項,接著「Next」連按完成安裝
          只勾選這二項,接著「Next」連按,完成安裝




                                   12
安裝Git
? 安裝tortoisegit




     「Next」連按完成安裝,裝完後記的重開機。
                              13
安裝Git
? 安裝完成後


          安裝成功的話,程式集中會出
          現「Git」、「TortoiseGit」二
          個目錄。




                                  14
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               15
Git指令教學
執行程式集中的「Git Bash」就可以用指令模
式操作Git了… : )




                       16
Git指令教學
最簡單的指令:git




             17
Git指令教學(help)
查指令怎麼用:git help [cmd]




                        18
Git指令教學(config)
? 設定Git系統參數
 – 設定自已在Git上的名字跟mail(用來區別自己和他人)
  git config --global user.name “my_name”
  git config --global user.email “my_name@email.com”
 – 查詢系統設定值:git config --global -l




                                                  19
Git指令教學(init)
建立repository:git init .




                          20
Git指令教學(init)
Repository結構




               頂層目錄叫做Working directory(工作目錄),
               除了「.git」子目錄外的所有檔案與目錄都是
               你正在編輯的資料(或開發的程式)

                                                21
Git指令教學(init)
Repository結構




               「.git」子目錄就是git repository。手動亂改裡
               面的資料,會發生悲劇,不要手賤亂搞它。



                                             22
Git指令教學(add/commit)
? 將working directory資料送進repository
  git add .
  git commit -a -m “my comment”




                                     23
Git指令教學(add/commit)
將working directory資料送進repository




              * 取自「Git in a nutshell」   24
Git指令教學(add/commit)
將working directory資料送進repository




                                   25
              * 取自「Git Tutorial」
Git指令教學(add/commit)
將working directory資料送進repository




                                   26
              * 取自「Git Tutorial」
Git指令教學(add/commit)
? 將working directory資料送進repository
  – git add .
     ? 將working directory裡「新增」與「修改」的檔案加到
       staging area
  – git commit -a -m “my comment”
     ? 將working directory裡「修改」與「刪除」的檔案加到
       staging area,接著把所有staging area的資料給commit到
       repository
  – 上面二個指令並用就能確保每次的commit都包含working
    directory裡所有「新增」、「修改」與「刪除」的檔案



                                            27
Git指令教學(add/commit)
? commit基本原則
 – 適當的粒度/相關性/獨立性
   ?   以一個小功能、小改進或一個bug fixed為單位
   ?   對應的unit test程式在同一個commit
   ?   無相關的修改不在同一個commit
   ?   語法錯誤的半成品程式不能commit
 – commit訊息很重要
   ? 第一行寫摘要
   ? 有需要寫實作細節的話,放第二行之後




              * 取自「Git Tutorial」   28
Git指令教學(status)
查看檔案狀態:git status




                    新增function1.txt檔案




                                        29
Git指令教學(status)
查看檔案狀態:git status



             檔案狀態是”Untracked files”,這是什麼
             東東?




                                           30
Git指令教學(status)
查看檔案狀態:git status




* 圖表取自「Git Tutorial」   想起來了嗎?




                                31
Git指令教學(status)
查看檔案狀態:git status




* 圖表取自「Git Tutorial」




                       32
Git指令教學(status)
查看檔案狀態:git status




* 圖表取自「Git Tutorial」




                       33
Git指令教學(status)
查看檔案狀態:git status




* 圖表取自「Git Tutorial」




                       34
Git指令教學(status)
查看檔案狀態:git status




* 圖表取自「Git Tutorial」




                       35
Git指令教學(gitignore)
? .gitignore
  – 告訴Git哪些檔案類型不用進版控




  – .gitignore大集合
     ? https://github.com/github/gitignore




                                             36
Git指令教學(diff/show)
? commit之前看
  檔案內容差異
 – git diff
 – git diff [file_name]
? commit之後看
  檔案內容差異
 – git show
 – git show HEAD^
 – git show [file_name]


                          37
Git指令教學(diff/show)
? commit之前看
  檔案內容差異
 – git diff
 – git diff [file_name]
? commit之後看
  檔案內容差異
 – git show
 – git show HEAD^
 – git show [file_name]


                          38
Git指令教學(diff/show)
? commit之前看
  檔案內容差異
 – git diff
 – git diff [file_name]
? commit之後看
  檔案內容差異
 – git show
 – git show HEAD^
 – git show [file_name]


                          39
Git指令教學(log)
查看commit的歷史記錄
? git log
? git log [file_name]
? git log --graph --decorate --all




                                     40
Git指令教學(reset)
? 檔案內容改爛了!怎麼辦?
 – commit之前改爛了,恢復到修改前的內容
  ? git checkout [file_name]
  ? git reset --hard
 – commit之後後悔了,恢復到前一版的commit
  ? git reset --hard HEAD^




                               41
Git指令教學(reset)
? 檔案內容改爛了!怎麼辦?
 – 未commit前改爛了,恢復
   到修改前的內容
     ? git checkout [file_name]
     ? git reset --hard
 – commit之後後悔了,恢復
   到前一版的commit
     ? git reset --hard HEAD^




執行後,前一版的
commit(246924f18f45bef4cee3
c97de023edeb9468cd05)砍
掉。這招起手無回,砍掉後救
不回來,使用前請三思。
                                  42
Git指令教學(branch)
為何要使用branch(分支)?




          * 取自「Git in a nutshell」   43
Git指令教學(branch)
使用branch(分支)




           * 取自「Git in a nutshell」   44
Git指令教學(branch)
使用branch(分支)


                                「master」branch



                              「new_feature1」branch




       「HEAD」代表目前所在位置。所以目前在
       「new_feature1」branch的最後一個commit           45
Git指令教學(branch)
? 列出所有branch
 git branch -a
? 產生新branch
 git branch [branch_name]
? 切換branch
 git checkout [branch_name]
? 刪除branch
 git branch -d [branch_name]



                               46
Git指令教學(branch)
列出所有branch:git branch -a




                           47
Git指令教學(branch)
產生新branch:git branch [branch_name]




                                     48
Git指令教學(branch)
切換branch:git checkout [branch_name]




         從「master」branch切到「new_feature2」branch




                                                 49
Git指令教學(branch)
刪除branch:git branch -d [branch_name]




                                       50
Git指令教學(tag)
? 幫特定commit進行”貼標籤”動作,一般用來把將要上
  線版本資料進行tag動作
? 列出所有tag
  git tag
? 產生新tag
  git tag [tag_name]
? 刪除tag
  git tag -d [tag_name]




                            51
Git指令教學(merge)
? 把其它branch接在current branch的尾巴
? 範例




                                 52
              * 取自「寫給大家的Git教學」
Git指令教學(merge)
? merge時衝到了(conflict),怎麼辦?




              * 取自「寫給大家的Git教學」   53
Git指令教學(rebase)
? current branch的頭改接在其它branch之後
? 範例
  git checkout experiment
  git rebase master
rebase之前                rebase之後




                    * 取自「寫給大家的Git教學」   54
骋颈迟指令教学(尘别谤驳别/谤别产补蝉别)




         * 取自「寫給大家的Git教學」   55
骋颈迟指令教学(尘别谤驳别/谤别产补蝉别)
? 還是搞不清楚merge與rebase差異?
 – 多練習,多玩儿次就會懂。
 – 一般狀況下大多用到merge,不會rebase其實
   影響不大。
 – 除非你(妳)搞的清狀況,Production repository
   別亂 rebase。因此搞爛Production repository
   的話,把搞爛的人拖出來毒打一頓。




                                         56
骋颈迟指令教学(谤别尘辞迟别)




                     ? 又回到了這張圖… : )
                     ? 前述指令都圍繞在local
                       端,現在要講server端指
                       令

                                   57
* 取自「Git Tutorial」
骋颈迟指令教学(谤别尘辞迟别)
離題一下,看看github這玩意兒
? https://github.com
? 簡單來說就是商業公司提供的git host服務;大廟一
  間,超多專案放上面
? 談錢傷感情,不過github上只有public project host免
  費,private project host要錢,好在價格便宜




                                      58
骋颈迟指令教学(谤别尘辞迟别)
離題一下,看看github這玩意兒
? 建立repository(step1)




                        59
骋颈迟指令教学(谤别尘辞迟别)
離題一下,看看github這玩意兒
? 建立repository(step2)




                        60
骋颈迟指令教学(谤别尘辞迟别)
離題一下,看看github這玩意兒
? 建立repository(step3)




                        61
骋颈迟指令教学(谤别尘辞迟别)




                     我們在server端(github)建立了
                     remote repository,接著可以
                     在local端使用remote指令設
                     定server端名稱與位址


                                        62
* 取自「Git Tutorial」
骋颈迟指令教学(谤别尘辞迟别)
? remote指令用來設定local端要連線至server端
  時的連線位址
? 列出server端位址
  git remote -v
? 設定server端位址
 git remote add origin https://github.com/cloudtu/git-demo.git
? 刪除server端位址
  git remote rm origin



                                                             63
骋颈迟指令教学(谤别尘辞迟别)
列出server端位址:git remote -v




                            64
骋颈迟指令教学(谤别尘辞迟别)
設定server端位址
git remote add origin https://github.com/cloudtu/git-demo.git




                                                                65
骋颈迟指令教学(谤别尘辞迟别)
刪除server端位址:git remote rm origin




                                   66
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)




                     local端設定好server端位址後就可以
                     進行這二端點的資料同步(push/pull)




                                         67
* 取自「Git Tutorial」
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? 把local端資料推送到server端
 – 用push指令
   ? 推送所有branch到server:git push --all origin
   ? 推送所有tag到server:git push --tags origin
   ? 推送特定branch到server: git push origin master
? 把server端資料拉回並合併到local端
 – 用pull指令
   ? 拉回所有branch並合併到local:git pull origin
   ? 拉回特定branch並合併到local:git pull origin master
 – 拉回並合併後把local repository搞的亂七八糟,
   想要後悔!
   ? 那就退回這次的動作吧:git reset --hard HEAD^            68
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? push完成後的log



                server端remote branch也會記錄在
                log裡面




                                      69
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)




                            70
         * 取自「寫給大家的Git教學」
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? pull = fetch + merge




                * 取自「寫給大家的Git教學」
                                   71
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? pull - special case demo(step1)




                                    72
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? pull - special case demo(step2)




                          每個branch只有進行fetch,沒有
                          merge,要自己手動處理



                                             73
骋颈迟指令教学(辫耻蝉丑/辫耻濒濒)
? pull - special case demo(step3)

                     手動merge




                                    74
骋颈迟指令教学(肠濒辞苍别)




                     server端已存有remote repository,local
                     端可用clone指令複製一份到local端




                                                    75
* 取自「Git Tutorial」
骋颈迟指令教学(肠濒辞苍别)
? clone一份server端repository至local端
  git clone https://github.com/cloudtu/git-demo.git git-demo




                                                        76
骋颈迟指令教学(肠濒辞苍别)
? clone出來的repository裡,local branch只會有
  master branch



? 需要其它的local branch要自己建




                                    77
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               78
常用的骋颈迟指令




   * 取自「Git in a nutshell」   79
常用的骋颈迟指令
想要快速複習一下?




                   都放在那裡了,自己去拿吧!
                                                           80
http://cloudtu.blogspot.tw/2012/08/git-command-fast-memo.html
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               81
Git GUI工具教學(TortoiseGit)
 TortoiseGit跟檔案總管直接整合,按下滑鼠右
 鈕就可以直接使用… : )




                           82
Git GUI工具教學(TortoiseGit)
查TortoiseGit怎麼用




                           83
Git GUI工具教學(TortoiseGit)
? 設定Git系統參數
 – TortoiseGit   Settings




                            84
Git GUI工具教學(TortoiseGit)
建立repository




                           85
Git GUI工具教學(TortoiseGit)
將working directory資料送進repository




                                   86
Git GUI工具教學(TortoiseGit)
? .gitignore
  – 告訴Git哪些檔案類型不用進版控




                           87
Git GUI工具教學(TortoiseGit)
? commit之前看檔案內容差異
 – TortoiseGit   Diff




                           88
Git GUI工具教學(TortoiseGit)
? commit之後看檔案內容差異
 – TortoiseGit   Show Log




                            double click it



                                              89
Git GUI工具教學(TortoiseGit)
? 查看commit的歷史記錄
 – TortoiseGit   Show Log




                            90
Git GUI工具教學(TortoiseGit)
? 檔案內容改爛了!怎麼辦?
 – commit之前改爛了,恢復到修改前的內容
  ? TortoiseGit   Show Log   Revert




 – commit之後後悔了,恢復到前一版的commit
  ? TortoiseGit   Show Log   Reset




                                      91
Git GUI工具教學(TortoiseGit)
使用branch(分支)



               紅底方塊表示目前正在使用的branch


                綠底方塊表示目前未使用的branch


               黃底方塊表示tag




                                 92
Git GUI工具教學(TortoiseGit)
新增、刪除、查詢、切換branch




                           93
Git GUI工具教學(TortoiseGit)
新增、刪除、查詢tag




                           94
Git GUI工具教學(TortoiseGit)
? merge:把其它branch接在current branch的尾巴
? 範例




                                       95
Git GUI工具教學(TortoiseGit)
? rebase:current branch的頭改接在其它branch之後
? 範例




                                     96
Git GUI工具教學(TortoiseGit)
? remote指令用來設定local端要連線至server端
  時的連線位址
 – TortoiseGit   Settings




                              97
Git GUI工具教學(TortoiseGit)
? 把local端資料推送到server端
 – TortoiseGit   push




                           98
Git GUI工具教學(TortoiseGit)
? 把server端資料拉回並合併到local端
 – TortoiseGit   pull




                           99
Git GUI工具教學(TortoiseGit)
? push完成後的log




                server端remote branch也會記錄在
                log裡面




                                            100
Git GUI工具教學(TortoiseGit)
? clone一份server端repository至local端




                                    101
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               102
Git基本守則
? 編輯文字檔案請用UTF8編碼
? 設定.gitignore,只commit必要檔案
 – compiled binary、log 、temp file不要放到
   repository
? commit守則
 – 每次commit只改一件事情
 – 寫清楚commit message
? 別對production repository下你不熟的git指令
? 「千萬不要」對已經push的東西作rebase
                                        103
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               104
常用协同开发流程




                       105
    * 取自「寫給大家的Git教學」
Agenda
?   版控系统发展史
?   骋颈迟设计目标
?   安裝Git
?   Git指令教學
?   常用的骋颈迟指令
?   Git GUI工具教學(TortoiseGit)
?   Git基本守則
?   常用协同开发流程
?   參考資料
                               106
參考資料
? 寫給大家的Git教學
   – http://www.slideshare.net/littlebtc/git-5528339
? Git in a nutshell(投影片)
   – http://www.slideshare.net/Dannvix/git-in-a-nutshell
? Git in a nutshell(影片)
   – http://www.youtube.com/watch?v=1vkbR9itr0I
? Git Tutorial
   – http://www.slideshare.net/ihower/git-tutorial-13695342
? TortoiseGit Intro in Traditional Chinese
   – http://www.slideshare.net/jason8301/tortoisegit-intro-
     in-traditional-chinese
? Stackoverflow
   – http://stackoverflow.com/questions/tagged/git
                                                           107

More Related Content

Git tutorial for windows user (給 Windows user 的 Git 教學)