65
Git and Git Hub

Git and git hub

  • Upload
    -

  • View
    938

  • Download
    11

Embed Size (px)

DESCRIPTION

A simple introduction for basic use of git and git hub. Since I'm a rookie to Git, If there is anything wrong, please contact me. Hope you'll enjoy it.

Citation preview

Page 1: Git and git hub

Git and Git Hub

Page 2: Git and git hub

What’s Git

一套版本控制系統

Page 3: Git and git hub

What’s version control?

有沒有 code改著改著就回不去了的經驗 ?

Page 4: Git and git hub

What’s version control?

有沒有 code改著改著就回不去了的經驗 ?

你需要的就是版本控制 !!!

打 Game要存檔,打 Code當然也要存檔 !!!

Page 5: Git and git hub

What’s version control?

有沒有 code改著改著就回不去了的經驗 ?

你需要的就是版本控制 !!!

打 Game要存檔,打 Code當然也要存檔 !!!

那多複製個幾份不就好了 ?

Page 6: Git and git hub

What’s version control?

有沒有 code改著改著就回不去了的經驗 ?

你需要的就是版本控制 !!!

打 Game要存檔,打 Code當然也要存檔 !!!

那多複製個幾份不就好了 ? 占空間、難維護

Page 7: Git and git hub

What’s version control?

有沒有 code改著改著就回不去了的經驗 ?

你需要的就是版本控制 !!!

打 Game要存檔,打 Code當然也要存檔 !!!

那多複製個幾份不就好了 ? 占空間、難維護Git只會記錄每一次有差異的部分,但使用起來就像存了很多檔一樣

Page 8: Git and git hub

Before start

安裝 Git

設定基本資料

Page 9: Git and git hub

Install Git For Linux

Fedora

Debian, Ubuntu

yum install git-core

sudo apt-get install git

Page 10: Git and git hub

Install Git For Mac

使用MacPorts

MacPortshttp://www.macports.org/

使用 brew

brewhttp://brew.sh/

sudo port install git-core +svn +doc +bash_completion +gitweb

brew install git

Page 11: Git and git hub

Install Git For Windows

http://msysgit.github.com/到這裡下載吧 !

Page 12: Git and git hub

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 usergit config --global user.email user@user

Page 13: Git and git hub

Set alias

以自訂的短文字來取代長長的指令  -> 懶人必備 !!!  指令:

下面是我設定的 alias

git config --global alias."command alias"  "original command“

git config --global alias.st statusgit config --global alias.cmt commitgit config --global alias.cmtm "commit –m"git config –global alias.br branch

Page 14: Git and git hub

Seting

透過這個指令就能看到我們對 git做了哪些設定git config -- global –l

Page 15: Git and git hub

Let’s start a repo in local

What’s repo repository ,  git 都是這麼稱呼它的版本資料庫

將一個專案 (其實就是資料夾啦 )加入 git追蹤git init

Page 16: Git and git hub

Let’s start a repo in local

Page 17: Git and git hub

Let’s start a repo in local

出現了一個 .git的隱藏資料夾 !!! 這是儲存 git紀錄的地方,沒事不要去動它

Page 18: Git and git hub

Let’s start a repo in local

出現了一個 .git的隱藏資料夾 !!! 這是儲存 git紀錄的地方,沒事不要去動它

這是 oh my zsh的功能

Page 19: Git and git hub

Stage

透過這個指令可以看到現在檔案的狀態不過我們已經透過 alias把 status 設為 st了,所以以後就可以用那現在就要來講 git很重要的四個檔案狀態 (stage)

git status

git st

Page 20: Git and git hub

Stage

總共有分成 4個階段0. untracked (新的檔案,根本還沒加入整個 git版本控制的流程內 )

1. unmodified (完成 add ,但還沒 commit)

2. modified (add了之後又修改  )3. staged (commit完成 ,從此以後記錄檔就存好了 )

Page 21: Git and git hub

Stage - staged

所有東西都已經加入 git的追蹤  (其實根本就甚麼東西都還沒有 )

Page 22: Git and git hub

Stage - untracked

我們建立一個檔案叫做 hi.txt現在有一個檔案進入 untracked了

Page 23: Git and git hub

Stage - unmodified

  將新的 file加入 git 追蹤git add “file name”

Page 24: Git and git hub

Stage - modified

如果要一次加入很多檔案怎麼辦 ?

增加了 2個檔案

Page 25: Git and git hub

Stage - modified

如果要一次加入很多檔案怎麼辦 ?git add .

不過不建議這麼做通常還是一個一個 add會比較好

Page 26: Git and git hub

Stage - staged

Page 27: Git and git hub

讓剛剛的檔案進入 stage吧 !!!

你會進入一個編輯器,讓你輸入 commit

Stage - staged

git commit

預設的編輯器通常不是這個可以透過git config –global core.editor=vim來設定

Page 28: Git and git hub

Stage - staged

不過開啟編輯器有點麻煩透過這個指令,就能在指令列完成 commitgit commit –m “your commit for this change”

Page 29: Git and git hub

commit 要輸入什麼 ?你這次做了甚麼修改 ?就是你這次為什麼要存檔啦 !!!

Stage - staged

Page 30: Git and git hub

Stage - staged

上一個 commit錯了,怎麼辦 ?你需要的是修改剛剛 commit的內容git commit --amend

Page 31: Git and git hub

進入 staged之後這份修改就會永遠被存下來查看之前的 commit紀錄

Stage - staged

git log

按 q可以退出

Page 32: Git and git hub

Stage - modified

如果修改了 add過的檔案,就會變成這樣

這時候只要再把它 git add進去就可以了

Page 33: Git and git hub

Stage

新增檔案 => 加入  track (git add) => 提交  (git commit)=> 繼續加入或修改其他檔案

修改檔案 => 加入  stage (git add) => 提交 ( git commit )=> 繼續修改其他檔案

Page 34: Git and git hub

Restore

讓檔案回到最後 commit的狀態

將檔案回到modified (也就是取消 add)git reset “file name”

git checkout “file name"

Page 35: Git and git hub

Restore

經過了多次 commit,如果想回到很久以前的版本怎麼辦 ?

Page 36: Git and git hub

Restore

經過了多次 commit,如果想回到很久以前的版本怎麼辦 ?git reset “commit SHA前四碼”

這串就是每個 commit自己的 SHA

Page 37: Git and git hub

Restore

回到 first commit吧 !!!

這時候你的修改沒有被消除只是你的 commit回到了 first commit如果這時候 git checkout hi2.txt就會回朔到第一次 commit時, hi2.txt的狀態

Page 38: Git and git hub

Restore

如果後悔 reset了怎麼辦 ?沒關係,東西其實都還在,在 reset回去就好了

查看所有參照的日誌git reflog

在進行一次 reset就好了

Page 39: Git and git hub

Restore

這些長長的 SHA碼,看了就覺得煩 ? Git用了一些代號來表示前 n次的 commit HEAD : 最近一次的 commit HEAD~1 : 前兩次的 commit HEAD~n : 前 n+1次的 commit

git reset “HEAD~n”

Page 40: Git and git hub

Restore

只想讓某個檔案回到之前的狀態 ?

那麼這時候這個檔案進行 checkout就會回到這次 commit完成的狀態

reset不會改變你現在存在的檔案的狀態,改變的是前一次 commit指向的位置checkout才能把你現在的檔案回復到之前的狀態

???沒關係下一頁有例子

git reset “HEAD~n” “file name”

Page 41: Git and git hub

Reset V.S Checkout

hi2.txt在 fourth commit的內容

hi2.txt在 second commit的內容

Page 42: Git and git hub

Reset V.S Checkout

1. 在 hi2.txt又加入一個數字

Page 43: Git and git hub

Reset V.S Checkout

1. 在 hi2.txt又加入一個數字 2. reset到兩次以前  (second commit)

Page 44: Git and git hub

Reset V.S Checkout

1. 在 hi2.txt又加入一個數字 2. reset到兩次以前  (second commit)

3. hi2.txt的內容還是沒變

Page 45: Git and git hub

Reset V.S Checkout

1. 在 hi2.txt又加入一個數字 2. reset到兩次以前  (second commit)

3. hi2.txt的內容還是沒變 4. checkout

Page 46: Git and git hub

Reset V.S Checkout

1. 在 hi2.txt又加入一個數字 2. reset到兩次以前  (second commit)

3. hi2.txt的內容還是沒變 4. checkout 5. 回復到 second commit的內容了

Page 47: Git and git hub

Ignore file

有時候,有些檔案不想被加入 git追蹤怎麼辦 ?那就忽略它吧 !!!讓一些密碼或機密資料不要被追蹤和上傳

.gitignore內的檔案將不被 git追蹤,如果在資料夾內就要把資料夾也打上去e.g. 在 aFolder下的 b : echo aFolder/b > .gitignore

注意 .gitignore不用也不能加 ./注意這方法只適用在還沒加入追蹤的檔案

echo "filename" > .gitignore

Page 48: Git and git hub

Ignore file

如果想 ignore已經加入的 file怎麼辦 ?那就不要追蹤它了 !!!!!

先把它從版本資料庫移除接著在做上一頁的步驟,就可以 ignore了

git rm --cache “file name”

Page 49: Git and git hub

Rename(move) and delete files

刪除已經被加入 git追蹤的檔案

重新命名和移動位置對於 Linux而言,這兩件事基本上是相同的git mv “original filename” “new filename”

git rm "filename"

Page 50: Git and git hub

Branch

Git非常重要的功能可以想像成複製很多份同樣的 code各自做不同的修改通常是測試或者寫新功能的時候會用到

就是拿來解支線任務的功能啦 !!!切出不同的支線,每一個支線的存檔都能解不同的支線任務

Page 51: Git and git hub

Branch

開一個新的分支 (名稱自訂 )

切換到另一個分支git branch “branch name"

git checkout “branch name"

Page 52: Git and git hub

Merge

將另一個 branch合併到現在的 branch

通常做完 branch的功能後,會切回master才merge之後就能把合併完的 branch刪掉如果想要強制刪掉還沒merger的 branch

git merge “branch name”

git branch –d “branch name”

git branch –D “branch name”

Page 53: Git and git hub

Git Hub

What is Git Hub一個可以將原始碼開放到網路上的網站很多強大的開放專案都來自這 p.s.私有專案是要付費的

Page 54: Git and git hub

Create a repo in Git Hub

建立帳號後,登入會看到這個畫面

Page 55: Git and git hub

Create a repo in Git Hub

為你的 repo選個好名子,就可以 Create repository了 !!!

Page 56: Git and git hub

Create a repo in Git Hub

Page 57: Git and git hub

Push your repo

Push -> 簡單來說就是把你的檔案上傳到 git hub如果你還沒有一個 git 專案,你可以照著他打記得要把 origin後面的網址改成你的 repo的網址喔~

touch README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin https://github.com/Lee-W/tryGit.gitgit push -u origin master

Page 58: Git and git hub

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

Page 59: Git and git hub

What should be in a repo

你的程式碼 readme

右下角的 readme要用markdown的語法寫

Git Hub還有自己的一套markdownGit hub Favored Markdown

通常會命名為 readme.md 其實還有一些檔名 git hub可以讀到想知道就自己 google吧 !!!

Page 60: Git and git hub

Download a repo

這裡就要分成兩個支線了 1.如果你的電腦還沒有這個 repo -> Clone a repo 2.如果你的電腦已經有這個 repo了 -> Pull a repo

Page 61: Git and git hub

Clone a repo

如果你想要複製別人 (或自己 )的 repo可以使用git clone “repo url”

Page 62: Git and git hub

Pull a repo

如果你的電腦上已經有這個 repo了而且 remote也是設定在這個 repo上

這樣就可以把最新的版本抓下來git pull

Page 63: Git and git hub

Pull a branch in a remote repo

透過這兩個指令,就可以把 repo上特定的 repo抓下來git fetch origingit pull origin “branch_name”

Page 64: Git and git hub

Other Remote Operation

顯示現在 remote的點在哪刪除 remote的點加入 remote的點

git remote -v

git remote rm origin

git remote add origin “URL”