本篇文章操作指令均於Windows系統上測試,但理論上皆可在Linux、Windows、Mac OS上面運行
參考資料: 為你自己學 Git!
版本控制就是於不同時間點檔案變更的紀錄。
舉例而言,各位應該都有類似的經驗:
(圖片來源: https://backlog.com/git-tutorial/tw/intro/intro1_1.html)
這樣做的壞處是:
而Git就是為了解決這些問題而誕生。
免費、開源
速度快、檔案體積小
記錄檔案內容的「快照」(snapshot)而非整個目錄複製貼上
分散式系統
集中式版本控制系統,例如CVS或是SVN,都需要有一台專用的伺服器,所有的更新都需要跟這台伺服器溝通。也就是說,萬一這台伺服器壞了,或是沒有網路連線的環境,就沒辦法使用。
很多人應該都聽過GitHub,而Git與Github是不同的東西
聽說GitHub被微軟買走了,怎麼辦!!
別擔心,同類型的網站還有Gitlab等等,關鍵字: Github Alternative
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@gmail.com"
確認目前設定值:
$ git config --list
$ cd Destop/SIRLA/git #切換到目標目錄
$ mkdir git-practice #建立git-practice目錄
$ cd git-practice #切換到git-practice目錄
$ git init #初始化目錄,讓git對這個目錄進行版本控制
執行完後,資料夾中會多一個.git的資料夾,裡面存放的就是git用來進行版本控制的檔案,只要把這個資料夾刪除,git就會解除對這個資料夾的版本控制
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
$ echo "Hello, git." > "welcome.html"
git status
,會發現git追蹤到一個沒有在暫存區內的檔案
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
welcome.html
nothing added to commit but untracked files present (use "git add" to track)
$ git add welcome.html
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: welcome.html
這邊可以看到welcome.html變成new file的狀態,代表它已經在暫存區上了
$ git add --all
指令將資料夾內全部檔案放置到暫存區
$ git commit -m "initial commit" #將暫存區commit,並用-m參數加入說明
[master (root-commit) 4443229] initial commit
1 file changed, 1 insertion(+)
create mode 100644 welcome.html
完成這個動作之後,你的資料夾就成功的儲存了一次版本
如果有對檔案進行修改,則修改後的檔案需再上傳至暫存區之後,再進行commit。
可以看到下方,我對檔案進行修改後,Changes not staged 那邊有一個modified的welcome.html,代表它修改了之後還未加入暫存區。
如果這時候commit,提交的只有上一次放到暫存區裡的檔案,而不是這個編輯過的檔案。
所以請記得,commit之前先執行$ git add --all
$ echo "Hello, again." > welcome.html #修改檔案
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: welcome.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: welcome.html
$ git log
commit 4443229c2018ef511f283ae8469e855d8b8c5dab (HEAD -> master)
Author: Sam Yang <samabc75@gmail.com>
Date: Sun Oct 21 13:22:11 2018 +0800
initial commit
分幾個部分來看:
可加入–oneline引數,讓結果僅顯示commit訊息
C:\Users\Sam\Desktop\SIRLA\git\git-practice>git log --oneline
4443229 (HEAD -> master) initial commit
如果要顯示中文的話,請先在終端機執行以下指令,設定LC_ALL環境變數,讓git知道要用utf-8作為編碼,輸入完指令請重開終端機。
參考資料: https://blog.miniasp.com/post/2017/09/17/Git-for-Windows-Command-Prompt-Display-Chinese-Issues.aspx
SETX LC_ALL C.UTF-8
使用此指令:
$ git checkout filename #filename自己替換成你的檔名
如果要找回全部誤刪的檔案:
$ git checkout . #.代表全部
實際演示:
C:\Users\Sam\Desktop\SIRLA\git\git-practice>del welcome.html #刪除welcome.html
C:\Users\Sam\Desktop\SIRLA\git\git-practice>git status #觀看目前狀態,發現有個檔案被刪除了
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: welcome.html
no changes added to commit (use "git add" and/or "git commit -a")
C:\Users\Sam\Desktop\SIRLA\git\git-practice>git checkout welcome.html #使用checkout把檔案救回來
C:\Users\Sam\Desktop\SIRLA\git\git-practice>git status #再確認一次狀態,發現沒有改變什麼東西,被刪掉的東西回去了
On branch master
nothing to commit, working tree clean
這個指令也可以拿來還原回之前的指令
$ git checkout HEAD~1 #還原回前一個commit
GitHub提供了一個github page的功能,可以供使用者存放靜態網頁(純前端,沒有後端的網頁)
$ cd "your directory" #切換到你的資料夾中
$ git init #git初始化
$ git add --all #將所有檔案加入暫存區
$ git commit -m "initial commit" #提交至儲存區
$ git remote add origin https://github.com/Bluebell3310/Bluebell3310.github.io #新增遠端位置,名稱命名為origin,伺服器位置為後面那一串
$ git push -u origin master #將master分支中推向origin這個位置
$ git remote -v #查看你目前有麼remote
$ git remote rm origin #把你要移除的遠端repository移除
git push
的 "-u" 參數,是設定預設推出的分支與推向的位置,這行執行完之後,以後每次只需輸入git push
就會自動上傳同樣的分支到同樣的位置,假設沒執行這個指令,每次push皆須輸入git push origin master
想要從GitHub複製他人專案時:git clone 網址
例如想要複製我的部落格:
$ git clone https://github.com/Bluebell3310/Bluebell3310.github.io
輸入此指令就會將整個專案下載到你當前所在的資料夾
想要與他人共同合作進行專案
在最一開始時,先git clone將專案整份複製下來。之後,在開始進行任何工作之前必須先進行git pull
指令,這個指令會偵測哪裡有改動,並且自動與本機檔案進行合併。
如果之前沒有設定過upstream(參見git push那邊),這邊就須輸入git pull origin master #從origin位置拉下master分支
。