版本控制 使用git & git hub
DESCRIPTION
TRANSCRIPT
版本控制 - 使用Git & GitHub
2014 Spring Web Programming, NCCUAuthor: pa4373 (Licensed by CC-By 4.0)
http://goo.gl/qP4pjX
版本控制(Revision Control)
一個能夠記錄一個或一組檔案在某一段時間的變更,使得讀者以後能取回特定版本的系統。
(電動遊戲裡的記錄點)
使用情境
方便實驗不同程式碼
(把程式搞砸了,退回到之前記錄點就好)
使用情境
多人協同合作開發
(這bug是他提交的,你去找他把它修好)
使用情境
交程式作業(重要!)
(遲交、忘記附件、假壓縮檔、假連結說掰掰)
主流版本控制軟體
● CVS● Subversion● Mercurial● Git
Git
● 最初作為管理Linux核心開發用● 快速 (以C撰寫)● 設計簡單● 支援非線性開發(分支)● 完全的分散式系統● 支援大規模專案(Ex: Linux核心)
GitHub
● 儲存遠端版本庫的服務● Git的良好支持● 允許用戶評論代碼● bug回報機制● 免費(專案需開源)● 以Ruby on rails撰寫● 版本庫伺服器 + 專案管理工具
(多人協同開發)
Git & GitHub
Git是一個分佈式版本控制/軟件配置管理軟體
GitHub 是一個用於存放程式碼和專案的網路服
務,使用者使用Git和GitHub溝通
沒有GitHub,仍然可以使用Git
為何不使用GitHub客戶端?
其他程式碼託管服務(Bitbucket、Google Code)也支援Git,轉換容易
安裝Git
● Mac OS XOS X安裝開發者工具自帶Git,若想升級到新版可用macports, homebrew。或至官網下載(http://git-scm.com/download/mac)
● LinuxDebian-based: # apt-get install git
Redhat-based: # yum install git
Arch Linux: # pacman -S git
(Note: “#”和”$”是常見命令提示符,不需輸入)
● 從原始碼編譯● 在終端機下輸入git即可執行。
(OS X: 打開Terminal.app)
安裝Git
● Windows1. 到Git官網下載:
http://git-scm.com/download/win
2. 瘋狂按Next,除了下面這個畫面要注意(重要!)
安裝Git
LF, CRLF:不同的作業系統使用不同的換行符號:
Unix-based系統(Linux, OS X, *BSD): LF “\n”
Windows系統: CRLF “\r\n”
Windows瀏覽LF文件會全部擠成一行,
Unix-based系統瀏覽CRLF文件每行都會多出^M
Git自動轉換:當前圖選擇第一項時,執行 git commit 時會把文檔中的 CRLF 符號自動轉換成 LF 。而利用 git checkout 取出檔案到工作目錄時,則會自動將 LF 轉成 CRLF
Linux, OS X使用者和Windows使用者方便一同工作。
許多伺服器運行UNIX系統,正確的換行符能減少問題發生可能。
安裝Git
執行:開始 -> 所有程式 -> Git -> Git Bash在終端機輸入git
安裝Git
● 解決Windows下中文顯示、輸入問題:在終端機輸入以下指令:
$ curl -L http://goo.gl/JjKZF5 | sh
重啟Git Bash
● 解決Windows Git Bash上剪下貼上問題:在終端機標題列敲擊右鍵,選擇“內容”
勾選“快速編輯模式”
按”確定“
選擇“修改啟動這個視窗的捷徑”
以後直接拖拉就可以反白選取複製的內容,按右鍵剪下。
在正常的模式下,按右鍵就可貼上。
使用Git
● 熱身操:切換到工作目錄(cd)命令:$ cd <工作目錄路徑>
$ cd ~/foo/bar/project # 絕對路徑
$ cd ../bar/project # 相對路徑
給Windows使用者:Git Bash實際上是在Windows模擬一個UNIX的Bash (命令殼層),所以它的操作方法和Windows自帶的cmd.exe是不一樣的,而是相同於UNIX系統下的Bash。因此,Bash的表達檔案系統方式類似於UNIX系統以根目錄(Root, /)爲起始,以下爲幾個路徑的比較:
記得檢查有沒有操作當前工作目錄的權限
UNIX-Like (Git Bash) Windows
/c/ C:\
/c/Users/USERNAME/ (使用者家目錄,又以 ~ 當代表)
C:\Users\USERNAME\
使用Git
● 熱身操:相對路徑、絕對路徑/Users/pa4373/Desktop/PROGRAMMING.LANGUAGES 資料夾
.├── C (媽!我在這裡! )│ └── Ritchie-Kernighan-The_C_Programming_Language_2_ed_.pdf├── Compilers-Principles-Techniques-and-Tools-2nd.pdf├── Haskell│ ├── Learn.You.a.Haskell.for.Great.Good-Miran.Lipovac.pdf│ └── func.hs└── Scala
└── Programming_in_Scala-EN.pdf
3 directories, 5 files如果我們要去Haskell這個資料夾,如何做呢?
相對路徑:cd ../Haskell (從現在的工作目錄算起,注意: ..表示上一層目錄 )絕對路徑:cd /Users/pa4373/Desktop/PROGRAMMING.LANGUAGES/Haskell (從檔案系統最上層算起 )
使用Git
● 熱身操:lsls 能列出當前目錄的檔案(.mbppy)pa4373@pa4373-osx:~/Desktop/PROGRAMMING.LANGUAGES$ lsC HaskellCompilers-Principles-Techniques-and-Tools-2nd.pdf Scala
使用Git
在我們開始之前......
1. 輸入的參數,大小寫有差。打字時請特別小心,
妳我的青春時光都非常短暫。
2. 多多使用Tab自動完成功能
3. cd, ls是系統內建的指令,所以前面不用加git (不要打出 git cd 之類的)
4. 子曰:不在其位,不謀其政。做任何事先確認是不是在對的資料夾。
舉例來說,要操作git要cd到git的專案資料夾。
要建ssh keys(後面會提到)要cd到~/.ssh去。
不能在.ssh下面弄git
5. 遇到問題多多爬文多想想吧,正常來說直接照抄下面的指令是一定不會
過的。目的在讓大家理解概念、了解指令,才能融會貫通。
使用Git
● 工作目錄 (Working Directory)專案進行開發以及所有 Git 相關的操作的目錄。
● 版本庫 (Repository)Git用來儲存每個版本、索引資訊等檔案的資料夾,通常在工作目錄內,
使用者無須手動更動裡面的內容,
資料夾名稱為 .git (千萬不要刪掉他!!!)
.├── .git│ ├── HEAD│ ├── branches│ ├── config│ ├── description│ ├── hooks│ │ ├── applypatch-msg.sample│ │ ├── commit-msg.sample│ │ ├── post-update.sample│ │ ├── pre-applypatch.sample│ │ ├── pre-commit.sample│ │ ├── pre-push.sample│ │ ├── pre-rebase.sample│ │ ├── prepare-commit-msg.sample│ │ └── update.sample│ ├── info│ │ └── exclude│ ├── objects│ │ ├── info│ │ └── pack│ └── refs│ ├── heads│ └── tags└── hello.hs
使用Git
$ git init這個命令會在當前的工作目錄下建立一個版本庫資料夾(.git),所有git指令造成的更動都會記錄在這個資料夾中
創建一個資料夾,然在在裡面輸入初始化的命令。
vagrant@debian-7:~/project$ git init
Initialized empty Git repository in /home/vagrant/project/.git/
vagrant@debian-7:~/project$
使用Git
$ git status這個命令會告知使用者當前版本專案的狀態
由於我們還沒新增任何的檔案,所以此地空空如也。
vagrant@debian-7:~/project$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to
track)
vagrant@debian-7:~/project$
使用Git
讓我們來新增一些檔案。
打開你最喜歡的編輯器,輸入以下文字:
儲存為index.html,記得確保你的檔名以htm或html,而沒有被Mac或Windows自動隱藏副檔名。(Help: OS X | Windows)
或偷懶直接使用echo命令(反正才一行嘛~)
<h1>Dead Simple Web Page</h1>
vagrant@debian-7:~/project$ echo "<h1>Dead Simple Web Page</h1>" > index.html
使用Git
用瀏覽器打開會長這樣
使用Git
$ git add <FILENAME>這個命令會在讓git掃描指定檔案是否有變化(更新、修改、刪除等等)。
特別注意的是用”.”來當作參數,意指掃描當前工作目錄和子目錄的所有檔案變化,我們常用git add .來直接操作(因為大家都很懶)。
承接上一個例子,在我們創建index.html後,是時候讓git知道新朋友的加入了,輸入git add .後按enter,沒有任何事發生,莫急莫慌莫害怕,這是因為git開發者遵循UNIX系統設計的哲學:沒消息就是好消息(參見Eric Raymond的The Art of Unix Programming: No
News is Good News),不愧是開發Linux系統內核的同一批人。
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$
使用Git
如果還是很怕,我們可以git status一下
vagrant@debian-7:~/project$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
#
vagrant@debian-7:~/project$
使用Git
git add本身只是告訴git檔案的存在,並不會馬上寫入版本庫。
我們再新增一個檔案,”git add .”後用”git status”查看結果
vagrant@debian-7:~/project$ echo "<h1>Why am I taking this class?</h1>" >
why.html
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
# new file: why.html
#
vagrant@debian-7:~/project$
使用Git
$ git commit -m "PUT YOUR COMMENT HERE"這個命令會提交檔案變化到git的版本庫。
git add只是單純的追蹤檔案變化,git commit這個指令才會實際的把檔案寫入版本庫。 -m 選項是必須的,在裡面你可以輸入這個版本更動的提示訊息。
vagrant@debian-7:~/project$ git commit -m "ready for gh-pages purposes"
[master (root-commit) 89ef72e] ready for gh-pages purposes
Committer: Vagrant User <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 why.html
vagrant@debian-7:~/project$
使用Git
Committer: 提交者
提交者訊息至關重要,它可以表明這份代碼更動的負責人是誰,git會根據你登入作業系統的使用者名稱以及主機名稱自動猜測,但如此並非總是準確。
透過以下命令,可以根據系統使用者,設定Committer訊息,這項行為在每台電腦只需做一次:
$ git config --global user.name “Your Name”
$ git config --global user.email [email protected]
完成之後,使用下面訊息更動目前專案的Committer訊息:$ git commit --amend --reset-author -m “Change committer info
for previous commits” (這行也是commit, 所以要加上comment)
Committer: Vagrant User <[email protected]>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly:
git config --global user.name "Your Name" git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
Q_Q 被神秘的畫面卡住
更改git committer資訊後讓之前的資訊也生效,我們會運行git commit --amend --reset-author‧有時會進到這個畫面(其他指令也有可能喔)
然後在這裡就不知道怎麼辦啦,亂按也出不去‧
這其實是一個操作比較特別的文字編輯器,叫做VIM‧ (wiki, 互動教學) 雖然很好很強大卻有點難學,這裡只教大家如何跳出來 XD
按esc -> 輸入 :q! -> 按enter
使用Git
$ git log這個命令會顯示最近的commit列表,以及他們的SHA1雜湊值。雜湊值能表示這次commit,在往後的操作會用雜湊值來辨認:
vagrant@debian-7:~/project$ git log
commit 891940d0272102d8b56815c059de7a807fd58807
Author: Vagrant User <[email protected]>
Date: Tue Feb 11 14:28:27 2014 +0000
ready for gh-pages purposes
vagrant@debian-7:~/project$
使用Git
讓我們來搞砸一些檔案吧!
在why.html添加第二行”<h3>Does Git really works?</h3>”
vagrant@debian-7:~/project$ echo "<h3>Does Git really works?</h3>" >>
why.html
使用Git
$ git reset --hard <COMMIT_SHA>這個命令會還原到指定commit的狀態,且刪除commit之後的記錄。(更多的還原方法看這
裡)
vagrant@debian-7:~/project$ cat why.html<h1>Why am I taking this class?</h1><h3>Does Git really works?</h3>vagrant@debian-7:~/project$ git reset --hardHEAD is now at 891940d ready for gh-pages purposesvagrant@debian-7:~/project$ cat why.html<h1>Why am I taking this class?</h1>vagrant@debian-7:~/project$
看指令看到睡著了嗎?讓我們暫時中場休息一下
使用GitHub
2. 選擇你要使用的付費方案,既然我們是窮學生選Free就足以
使用GitHub
3. Ta-dah!
使用GitHub
現在我們需要讓Git能和GitHub相互溝通:
1. HTTPS (使用密碼, email地址和GitHub伺服器驗證)2. SSH (使用金鑰和和GitHub伺服器驗證)
考慮到HTTPS跨平台設定不同、我們使用SSH認證做示範(有興
趣設定HTTPS者可參考這裡)
使用GitHub
1. 產生一對金鑰(Pair of Key)金鑰分成兩隻,公鑰(Public Key)用來加密,私鑰(Private Key)用來解密。
SSH會在~/.ssh這資料夾下儲存你的私鑰,讓你在登入時使用。
但一開始我們需要先打造這兩把鑰匙:
1. ~/.ssh不是一個git工作目錄,在裡面打git會GG噢~
2. 找不到~/.ssh怎麼辦?自己建一個吧:mkdir ~/.ssh (mkdir是啥)vagrant@debian-7:~$ cd .ssh/
vagrant@debian-7:~/.ssh$ ls
authorized_keys
vagrant@debian-7:~/.ssh$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx [email protected] key's randomart image is:
+--[ RSA 2048]----+
============================以下省略============================
使用GitHub
注意:#開頭的句子不需要輸入(井字號也不用)
vagrant@debian-7:~$ cd .ssh/ #切換到.ssh資料夾
vagrant@debian-7:~/.ssh$ ls #列出資料夾中檔案
authorized_keys
vagrant@debian-7:~/.ssh$ ssh-keygen -t rsa -C "[email protected]" #產生一組金
鑰,參數C填入你在github註冊的電郵地址
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): #使用預設值
Enter passphrase (empty for no passphrase): #直接enter不用口令,要增強安全性看這裡
Enter same passphrase again: #直接enter
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
#============================以下省略============================
使用GitHub
2. 告知GitHub公鑰將公鑰(以.pub結尾的檔案)內容暫存到剪貼簿
vagrant@debian-7:~/.ssh$ cat id_rsa.pub
ssh-rsa
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxx
xxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxx+xxxx
x+xxxxxxxxxxxxxxxxxx+xx+xxxxxxxxx+x+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxx+xxxxxxxxxx/xxxxxxxxxxxxxxxx wp2014nccu@gmail.
com
使用GitHub
3. 將公鑰添加到GitHub中1. 到GitHub的Accounts Settings頁面
2. 點選左方工具欄的SSH Keys
使用GitHub
3. 將公鑰添加到GitHub中3. 點選Add SSH Key
4. 將公鑰內容貼到Key Field欄位
5. 點選Add Key
使用GitHub
4. 測試GitHub公鑰運作正常
vagrant@debian-7:~$ ssh -T [email protected] # 這裡不用換,保留[email protected]
The authenticity of host 'github.com (192.30.252.130)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of
known hosts.
Hi wp2014nccu! You've successfully authenticated, but GitHub does not provide
shell access.
vagrant@debian-7:~$
用Git把程式碼送上GitHub
目前為止,你所有的git操作都在本地(local)進行。
現在我們要在GitHub上建立一個版本庫。
用Git把程式碼送上GitHub
1. 建立一個遠端版本庫(Remote Repo)在github個人頁面下方點擊New repository
用Git把程式碼送上GitHub
1. 建立一個遠端版本庫(Remote Repo)在Repository name輸入版本庫的名稱,使用Public才可不需付費。
其他設定都是可選的,完成後點選Create repository
用Git把程式碼送上GitHub
Ta-dah! 等等我們就要把本地的版本庫推到GitHub伺服器上了,
在此之前,請先按HTTP, SSH兩個按鈕中的”SSH”,並複製右邊的地址
用Git把程式碼送上GitHub
4. 將本機的版本庫推到github上
# 讓本機版本庫得知遠端版本庫的存在, origin後面接剛剛複製的網址
vagrant@debian-7:~/project$ git remote add origin [email protected]:wp2014nccu/hello-github.git# 將本機版本庫推上遠端
vagrant@debian-7:~/project$ git push -u origin masterWarning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (4/4), 331 bytes, done.Total 4 (delta 0), reused 0 (delta 0)To [email protected]:wp2014nccu/hello-github.git * [new branch] master -> masterBranch master set up to track remote branch master from origin.vagrant@debian-7:~/project$
用Git把程式碼送上GitHub
用Git把程式碼送上GitHub
$ git push, git pullgit push這個命令會將這次本地修改的變化(commit)同步到別的版本庫,git pull則是會將別的版本庫的commit抓回來,依據你的設定(和git remote有關)
用Git把程式碼送上GitHub
我改變了index.html檔案內容,做了一次commit,在push上GitHub。
vagrant@debian-7:~/project$ echo "When I'm producing the ppt, I stay up late cause I
messed around at day." > index.html
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$ git commit -m "How the ppt was made."
[master 82e0287] How the ppt was made.
1 file changed, 1 insertion(+), 1 deletion(-)
vagrant@debian-7:~/project$ git push
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the
list of known hosts.
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 350 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:wp2014nccu/hello-github.git
891940d..82e0287 master -> master
用Git把程式碼送上GitHub
使用Git
分支(Branch)有時候要開發新功能,有時候是要修正某個Bug,有時候想要測試某個特異功能能不能 work ,這時候我們通常都會從主 branch 再開出一條新的 branch 來做,這支新開的 branch 會帶著你的主 branch 目前的最新狀態,當你完成你所要開發的新功能/ Bug 修正後確認沒問題就再把它 merge(合併)回主 Branch ,如此便完成了新功能的開發..........
使用Git
使用Git
用GitHub提供的Branch來架網站
GitHub提供一個特別的Branch,叫做gh-pages。將網頁資料放在這個Branch裡面。你就能透過瀏覽器直接訪問這些內容(http://pages.github.com/)
https://github.com/username/repository/->
http://username.github.io/repository
用GitHub提供的Branch來架網站
開始之前,請先確定本機和遠端倉庫同步(使用git push和git pull)
# 使用git checkout 建立一個branch,orphan參數意指建立一個毫無相關的分支
vagrant@debian-7:~/project$ git checkout --orphan gh-pages
Switched to a new branch 'gh-pages'
# 沒有任何的commit
vagrant@debian-7:~/project$ git log
fatal: bad default revision 'HEAD'
# 做一次branch的第一次commit
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$ git commit -m "First commit to gh-pages"
[gh-pages (root-commit) 5120488] First commit to gh-pages
Committer: Vagrant User <[email protected]>
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 why.html
用GitHub提供的Branch來架網站
同步到github主機上,第一次push要等約莫10分鐘,之後都很快
# PUSH!
vagrant@debian-7:~/project$ git push origin gh-pages # 指定分支
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 367 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To [email protected]:wp2014nccu/hello-github.git
* [new branch] gh-pages -> gh-pages
vagrant@debian-7:~/project$
用GitHub提供的Branch來架網站
github上有了gh-pages這個分支
用GitHub提供的Branch來架網站
訪問網站: http://wp2014nccu.github.io/hello-github/
進階
多人協同開發
為什麼會有版本衝突?如何解決?
Branch的Mergegit rm和rm的差別?
資源
Pro Git: http://git-scm.com/book/zh (中文的教學書)Code School: https://www.codeschool.com/courses/try-git (互動式git教學)
Try Git on your project, and make a slide like me.(Pretty much how I learned git.)
QUESTION?
發問請上Connection平台
並隨餐附贈錯誤訊息(截圖、噴錯)問題盡量問清楚
能google看看,想過可能的原因更好
如果只有一句”跑不出來”,我想我也想不出來 =_=發問前請先看看其他人之前的發問,答案可能就在裡面
順手PrintScreen救救你和我
大家加油!