SlideShare una empresa de Scribd logo
1 de 66
Git and Git Hub
This version will no longer be maintained
Please checkout the new version of this slide
https://lee-w.github.io/git-tutorial/#/
What’s Git
 一套版本控制系統
What’s version control?
 有沒有code改著改著就回不去了的經驗?
What’s version control?
 有沒有code改著改著就回不去了的經驗?
你需要的就是版本控制!!!
 打Game要存檔,打Code當然也要存檔!!!
What’s version control?
 有沒有code改著改著就回不去了的經驗?
你需要的就是版本控制!!!
 打Game要存檔,打Code當然也要存檔!!!
那多複製個幾份不就好了?
What’s version control?
 有沒有code改著改著就回不去了的經驗?
你需要的就是版本控制!!!
 打Game要存檔,打Code當然也要存檔!!!
那多複製個幾份不就好了? 占空間、難維護
What’s version control?
 有沒有code改著改著就回不去了的經驗?
你需要的就是版本控制!!!
 打Game要存檔,打Code當然也要存檔!!!
那多複製個幾份不就好了? 占空間、難維護
Git只會記錄每一次有差異的部分,但使用起來就像存了很多檔一樣
Before start
 安裝Git
 設定基本資料
Install Git For Linux
 Fedora
 Debian, Ubuntu
yum install git-core
sudo apt-get install git
Install Git For Mac
 使用MacPorts
 MacPorts
http://www.macports.org/
 使用brew
 brew
http://brew.sh/
sudo port install git-core +svn +doc +bash_completion +gitweb
brew install git
Install Git For Windows
 http://msysgit.github.com/
到這裡下載吧!
Set e-mail and name
 一開始會要求要設定名稱和email才能使用
 一般來說就設定 git hub 或 bitbucket比較好
雖然也可以設定無效的,但push repo上去的時候就會是unknown上傳的
 指令:
 e.g.
git config --global user.name "<Your name>“
git config --global user.email "<Your email>"
git config --global user.name user
git config --global user.email user@user
Set alias
 以自訂的短文字來取代長長的指令 -> 懶人必備!!!
 指令:
 下面是我設定的alias
git config --global alias."command alias" "original command“
git config --global alias.st status
git config --global alias.cmt commit
git config --global alias.cmtm "commit –m"
git config –global alias.br branch
Seting
 透過這個指令就能看到我們對git做了哪些設定
git config -- global –l
Let’s start a repo in local
 What’s repo
 repository , git 都是這麼稱呼它的版本資料庫
 將一個專案(其實就是資料夾啦)加入git追蹤
git init
Let’s start a repo in local
Let’s start a repo in local
出現了一個.git的隱藏資料夾!!!
這是儲存git紀錄的地方,沒事不要去動它
Let’s start a repo in local
出現了一個.git的隱藏資料夾!!!
這是儲存git紀錄的地方,沒事不要去動它
這是oh my zsh的功能
Stage
 透過這個指令可以看到現在檔案的狀態
 不過我們已經透過alias把status 設為st了,所以以後就可以用
 那現在就要來講git很重要的四個檔案狀態(stage)
git status
git st
Stage
 總共有分成4個階段
0. untracked (新的檔案,根本還沒加入整個git版本控制的流程內)
1. unmodified (完成add ,但還沒commit)
2. modified (add了之後又修改 )
3. staged (commit完成 ,從此以後記錄檔就存好了)
Stage - staged
 所有東西都已經加入git的追蹤 (其實根本就甚麼東西都還沒有)
Stage - untracked
 我們建立一個檔案叫做hi.txt
 現在有一個檔案進入untracked了
Stage - unmodified
 將新的file加入git 追蹤
git add “file name”
Stage - modified
 如果要一次加入很多檔案怎麼辦?
增加了2個檔案
Stage - modified
 如果要一次加入很多檔案怎麼辦?
git add .
不過不建議這麼做
通常還是一個一個add會比較好
Stage - staged
 讓剛剛的檔案進入stage吧!!!
 你會進入一個編輯器,讓你輸入commit
Stage - staged
git commit
預設的編輯器通常不是這個
可以透過
git config –global core.editor=vim
來設定
Stage - staged
 不過開啟編輯器有點麻煩
透過這個指令,就能在指令列完成commit
git commit –m “your commit for this change”
 commit 要輸入什麼?
 你這次做了甚麼修改?
 就是你這次為什麼要存檔啦!!!
Stage - staged
Stage - staged
 上一個commit錯了,怎麼辦?
 你需要的是
 修改剛剛commit的內容
git commit --amend
 進入staged之後這份修改就會永遠被存下來
 查看之前的commit紀錄
Stage - staged
git log
按q可以退出
Stage - modified
 如果修改了add過的檔案,就會變成這樣
 這時候只要再把它git add進去就可以了
Stage
 新增檔案 => 加入 track (git add) => 提交 (git commit)
=> 繼續加入或修改其他檔案
 修改檔案 => 加入 stage (git add) => 提交( git commit )
=> 繼續修改其他檔案
Restore
 讓檔案回到最後commit的狀態
 將檔案回到modified (也就是取消add)
git reset “file name”
git checkout “file name"
Restore
 經過了多次commit,如果想回到很久以前的版本怎麼辦?
Restore
 經過了多次commit,如果想回到很久以前的版本怎麼辦?
git reset “commit SHA前四碼”
這串就是每個commit自己的SHA
Restore
 回到first commit吧!!!
這時候你的修改沒有被消除
只是你的commit回到了first commit
如果這時候git checkout hi2.txt
就會回朔到第一次commit時,hi2.txt的狀態
Restore
 如果後悔reset了怎麼辦?
 沒關係,東西其實都還在,在reset回去就好了
 查看所有參照的日誌
git reflog
在進行一次reset就好了
Restore
 這些長長的SHA碼,看了就覺得煩?
 Git用了一些代號來表示前n次的commit
 HEAD : 最近一次的commit
 HEAD~1 : 前兩次的commit
 HEAD~n : 前n+1次的commit
git reset “HEAD~n”
Restore
 只想讓某個檔案回到之前的狀態?
 那麼這時候這個檔案進行checkout就會回到這次commit完成的狀態
 reset不會改變你現在存在的檔案的狀態,改變的是前一次commit指向的位
置
checkout才能把你現在的檔案回復到之前的狀態
 ???
 沒關係下一頁有例子
git reset “HEAD~n” “file name”
Reset V.S Checkout
hi2.txt在fourth commit的內容
hi2.txt在second commit的內容
Reset V.S Checkout
1. 在hi2.txt又加入一個數字
Reset V.S Checkout
1. 在hi2.txt又加入一個數字 2. reset到兩次以前 (second commit)
Reset V.S Checkout
1. 在hi2.txt又加入一個數字 2. reset到兩次以前 (second commit)
3. hi2.txt的內容還是沒變
Reset V.S Checkout
1. 在hi2.txt又加入一個數字 2. reset到兩次以前 (second commit)
3. hi2.txt的內容還是沒變 4. checkout
Reset V.S Checkout
1. 在hi2.txt又加入一個數字 2. reset到兩次以前 (second commit)
3. hi2.txt的內容還是沒變 4. checkout 5. 回復到second commit的內容了
Ignore file
 有時候,有些檔案不想被加入git追蹤怎麼辦?
 那就忽略它吧!!!
 讓一些密碼或機密資料不要被追蹤和上傳
 .gitignore內的檔案將不被git追蹤,如果在資料夾內就要把資料夾也打上去
e.g. 在aFolder下的b : echo aFolder/b > .gitignore
 注意.gitignore不用也不能加./
 注意這方法只適用在還沒加入追蹤的檔案
echo "filename" > .gitignore
Ignore file
 如果想ignore已經加入的file怎麼辦?
 那就不要追蹤它了!!!!!
 先把它從版本資料庫移除
 接著在做上一頁的步驟,就可以ignore了
git rm --cache “file name”
Rename(move) and delete files
 刪除已經被加入git追蹤的檔案
 重新命名和移動位置
 對於Linux而言,這兩件事基本上是相同的
git mv “original filename” “new filename”
git rm "filename"
Branch
 Git非常重要的功能
 可以想像成複製很多份同樣的code各自做不同的修改
 通常是測試或者寫新功能的時候會用到
 就是拿來解支線任務的功能啦!!!
 切出不同的支線,每一個支線的存檔都能解不同的支線任務
Branch
 開一個新的分支(名稱自訂)
 切換到另一個分支
git branch “branch name"
git checkout “branch name"
Merge
 將另一個branch合併到現在的branch
 通常做完branch的功能後,會切回master才merge
 之後就能把合併完的branch刪掉
 如果想要強制刪掉還沒merger的branch
git merge “branch name”
git branch –d “branch name”
git branch –D “branch name”
Git Hub
 What is Git Hub
 一個可以將原始碼開放到網路
上的網站
 很多強大的開放專案都來自這
 p.s.私有專案是要付費的
Create a repo in Git Hub
 建立帳號後,登入會看到這個畫面
Create a repo in Git Hub
 為你的repo選個好名子,就可以Create repository了!!!
Create a repo in Git Hub
Push your repo
 Push -> 簡單來說就是把你的檔案上傳到git hub
 如果你還沒有一個git 專案,你可以照著他打
記得要把origin後面的網址改成你的repo的網址喔~
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Lee-W/tryGit.git
git push -u origin master
Push your repo
 比較重要的是這兩行
 git remote是將遠端設定為你的repo,只需要設定一次
 git push –u origin master就是把master分支傳到git hub上
當然也可以傳其他的branch
git remote add origin “Your repo”
git push -u origin master
What should be in a repo
 你的程式碼
 readme
 右下角的readme
 要用markdown的語法寫
Git Hub還有自己的一套markdown
Git hub Favored Markdown
 通常會命名為readme.md
其實還有一些檔名git hub可以讀到
想知道就自己google吧!!!
Download a repo
 這裡就要分成兩個支線了
 1.如果你的電腦還沒有這個repo -> Clone a repo
 2.如果你的電腦已經有這個repo了 -> Pull a repo
Clone a repo
 如果你想要複製別人(或自己)的repo可以使用
git clone “repo url”
Pull a repo
 如果你的電腦上已經有這個repo了
 而且remote也是設定在這個repo上
 這樣就可以把最新的版本抓下來
git pull
Pull a branch in a remote repo
 透過這兩個指令,就可以把repo上特定的repo抓下來
git fetch origin
git pull origin “branch_name”
Other Remote Operation
 顯示現在remote的點在哪
 刪除remote的點
 加入remote的點
git remote -v
git remote rm origin
git remote add origin “URL”
Other resource
 這裡附上一些很棒的的git教學和資源
 1. 寫給大家的 Git 教學
 2. Git教學
 3. 版本控制系統 Git 精要使用讓 Git 1.8 (iHower)
 4. 30 天精通 Git 版本控管
 5. Pro GIT (中譯)
 6. Try Git (互動)
 7. Learn Git Branching (互動)

Más contenido relacionado

La actualidad más candente

git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學
Doremi Lin
 
Yet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom up
Wen-Tien Chang
 
Mercurial簡介與教學
Mercurial簡介與教學Mercurial簡介與教學
Mercurial簡介與教學
芳本 林
 

La actualidad más candente (20)

Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作
 
A successful git branching model 導讀
A successful git branching model 導讀A successful git branching model 導讀
A successful git branching model 導讀
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战
 
Git 經驗分享
Git 經驗分享Git 經驗分享
Git 經驗分享
 
寫給大家的 Git 教學
寫給大家的 Git 教學寫給大家的 Git 教學
寫給大家的 Git 教學
 
git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學git, repo, Gerrit 基礎教學
git, repo, Gerrit 基礎教學
 
Git & git flow
Git & git flowGit & git flow
Git & git flow
 
Git與source tree 基礎教學
Git與source tree 基礎教學Git與source tree 基礎教學
Git與source tree 基礎教學
 
Xcode 的 git 版本管理
Xcode 的 git 版本管理Xcode 的 git 版本管理
Xcode 的 git 版本管理
 
Yet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom up
 
Git and Github basic with SourceTree
Git and Github basic with SourceTreeGit and Github basic with SourceTree
Git and Github basic with SourceTree
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹
 
Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰Visual Studio 2015 與 Git 開發實戰
Visual Studio 2015 與 Git 開發實戰
 
幸福快樂的完美結局
幸福快樂的完美結局幸福快樂的完美結局
幸福快樂的完美結局
 
Git 入門與實作
Git 入門與實作Git 入門與實作
Git 入門與實作
 
Gitlab
GitlabGitlab
Gitlab
 
初心者 Git 上手攻略
初心者 Git 上手攻略初心者 Git 上手攻略
初心者 Git 上手攻略
 
開發用不著打一架 - 分散式版本控制 Git
開發用不著打一架 - 分散式版本控制 Git開發用不著打一架 - 分散式版本控制 Git
開發用不著打一架 - 分散式版本控制 Git
 
Mercurial簡介與教學
Mercurial簡介與教學Mercurial簡介與教學
Mercurial簡介與教學
 
git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用
 

Destacado (6)

Open Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git HubOpen Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git Hub
 
LTE and LTE Advanced Introduction
LTE and LTE Advanced IntroductionLTE and LTE Advanced Introduction
LTE and LTE Advanced Introduction
 
LTE and LTE advanced Performance ( By 3GPP RAN Chairmans’ )
LTE and LTE advanced Performance ( By 3GPP RAN Chairmans’ ) LTE and LTE advanced Performance ( By 3GPP RAN Chairmans’ )
LTE and LTE advanced Performance ( By 3GPP RAN Chairmans’ )
 
LTE ADVANCED PPT
LTE ADVANCED PPTLTE ADVANCED PPT
LTE ADVANCED PPT
 
LTE Architecture and interfaces
LTE Architecture and interfacesLTE Architecture and interfaces
LTE Architecture and interfaces
 
Lte Presentation.Ppt
Lte Presentation.PptLte Presentation.Ppt
Lte Presentation.Ppt
 

Similar a Git and git hub

Git使用入门
Git使用入门Git使用入门
Git使用入门
dpf2e
 
Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀
Wen-Tien Chang
 
Git内部培训文档
Git内部培训文档Git内部培训文档
Git内部培训文档
superwen
 

Similar a Git and git hub (20)

GIT實務操作與理論
GIT實務操作與理論GIT實務操作與理論
GIT實務操作與理論
 
Git原理与实战 201607
Git原理与实战 201607Git原理与实战 201607
Git原理与实战 201607
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
Git入門介紹
Git入門介紹Git入門介紹
Git入門介紹
 
Git+使用教程
Git+使用教程Git+使用教程
Git+使用教程
 
Git 教學
Git 教學Git 教學
Git 教學
 
Git Essence Tutorial
Git Essence TutorialGit Essence Tutorial
Git Essence Tutorial
 
Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)Git 超簡單學習懶人包(軟體程式版本控管系統)
Git 超簡單學習懶人包(軟體程式版本控管系統)
 
Git 使用介绍
Git 使用介绍Git 使用介绍
Git 使用介绍
 
Git使用入门
Git使用入门Git使用入门
Git使用入门
 
Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀
 
COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報COSCUP 2015 開源之道-Git工作坊教學簡報
COSCUP 2015 開源之道-Git工作坊教學簡報
 
Git教學
Git教學Git教學
Git教學
 
Git簡報
Git簡報Git簡報
Git簡報
 
Git &amp; git hub v1.2
Git &amp; git hub v1.2Git &amp; git hub v1.2
Git &amp; git hub v1.2
 
Git introduction
Git introductionGit introduction
Git introduction
 
Learn git
Learn gitLearn git
Learn git
 
Git flow
Git flowGit flow
Git flow
 
Git share
Git shareGit share
Git share
 
Git内部培训文档
Git内部培训文档Git内部培训文档
Git内部培训文档
 

Git and git hub