如何使用版本控制(Git)-使用Bash

對於想要利用Git的新手(我就是), 剛進來學卻不是那麼容易,
因此將自己從不懂到有一點基礎的學習模式,
寫出來參考看看, 或許也可以幫助到一些想學卻不得其門而入的新手。

我們都知道版本控制的好處,
可是在Google完了, 會發現一堆指令看的眼花撩亂,
導致才剛想學, 就馬上產生放棄的念頭。

參考如何使用版本控制(Git)-安裝流程 進行安裝,
安裝完了以後, 我們就可以啟動 Git 的 Bash模式,

在這邊會有一些Linux指令的觀念,
因為Git本來就是使用在Linux上面的東西, 後來廣泛使用在許多平台上面,
所以還是學一下Linux基本指令比較好。

一開始先建立一個資料夾, 叫做GitDemo(當然你可以取自己想取的名稱),
mkdir GitDemo

切換到GitDemo裡面, 然後下 git init . 這個指令,
cd GitDemo
git init .
"." 代表著把這個資料夾當成檔案櫃(Repository)來使用,
然後就會出現這樣的訊息, 代表成功建立一個檔案櫃了
Initialized empty Git repository in c:/GitDemo/.git/

這時候我們可以建立一個檔案,
cat > myFile.txt
givemepass
說明一下,cat是可以檢視一個檔案的內容,
然而 ">" 符號是代表將接下來我們所輸入的資料,
寫入我們後面指定的檔案裡面,
因此我們輸入完givemepass這串文字以後, 按下ctrl + c 就可以結束輸入文字,
你只要輸入 ls 就可以看到資料夾下面多了一個檔案叫做 myFile.txt了。

git的觀念是這樣, 我們會有一個追蹤名單(stage area)來存放我們加入的追蹤,
當我們如果想把那些將來會變動的檔案, 加到追蹤名單裡面, 就會下
git add myFile.txt

那如果我們想查詢有哪些檔案是放在追蹤裡面,
就可以下
git status
這時候就可以看到一些訊息
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   myFile.txt
#
代表我們有一個檔案被放進追蹤名單, 到時候如果我們提交(Commit),
Git就會把現在在追蹤名單的檔案目前的狀態記錄起來。

我們可以在建立一個新檔案, 測試看看如果沒有放進追蹤,
會出現什麼樣的訊息。
cat > newFile.txt
This is test.
$ ls
myFile.txt  newFile.txt
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   myFile.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newFile.txt
當我們建立newFile.txt的時候, 並且寫入"This is test."的訊息,
接著我們就觀看追蹤名單的狀態,
出現我們有兩個檔案, 一個是放在追蹤名單, 一個是放在不追蹤。

那如果我們想要將追蹤名單的檔案移除,
就可以下指令如下:
git rm --cached myFile.txt
rm 'myFile.txt'
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       myFile.txt
#       newFile.txt
nothing added to commit but untracked files present (use "git add" to track)
兩個檔案都被移到非追蹤名單內了。

那如果我們想要把所有非追蹤的檔案, 都加到追蹤名單呢?
不可能叫你一個一個移, 可以利用 '.' 來加入所有的檔案。
git add .git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   myFile.txt
#       new file:   newFile.txt
#
意思就是將當前目錄底下所有的檔案, 加入至追蹤名單。

那麼現在就是要學如何提交(commit),讓Git紀錄目前檔案的狀態的時候了。
指令很簡單,
git commit -m "test1"
[master (root-commit) c76d7f6] my first time commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 myFile.txt
 create mode 100644 newFile.txt
commit 後面會加上-m的參數, 代表message的意思,
他可以讓你在後面加上一些訊息, 通常這個訊息是要你寫入這次commit的目的,
那麼下次你就可以藉由這個註解來理解,這次修改的目的是什麼了。

然後你可以利用log來查看一共有多少個commit。
git log
commit c76d7f6a2172aabe79ff1f7f103ffedace526d7d
Author: givemepass <givemepass@hotmail.com>
Date:   Mon Dec 26 15:37:55 2011 +0800

    test1
commit後面會有一串編號, 是屬於這次commit的專屬編號,
之後如果要回復某一次的commit, 就是靠這個編號了。

接著我們對myFile.txt進行修改,
cat >> myFile.txt
abcdefg
$ cat myFile.txt
givemepass
abcdefg
$ git commit -a -m "test2"
[master 4e3c83a] test2
 1 files changed, 1 insertions(+), 0 deletions(-)
$ git log
commit 4e3c83aa5e71ffaa3790fd1e5ed45e713a7a6c06
Author: Rick Wu <givemepassxd999@gmail.com>
Date:   Tue Dec 27 16:45:14 2011 +0800

    test2

commit 9334a34c9946cd08abd3473f877bdcbbf637ea46
Author: Rick Wu <givemepassxd999@gmail.com>
Date:   Tue Dec 27 16:44:20 2011 +0800

    test1
一開始我們加字串"abcdefg"在myFile.txt的後面,
然後查看一下myFile.txt確定字串"abcdefg"有加在後面了,
進行commit的指令, 這邊多一個-a的參數,
代表將修改過後的myFile.txt加入追蹤名單,
咦? 前面不是已經加過了嗎?
原來只要修改過後, 他會自動跳離stage的區域, 因此我們會多一個參數,
讓他在次的進入到stage裡面,
然後我們就進行commit。

好了, 現在假設第二次的commit以後, 我們想回到第一次commit的狀態,
這時候只要下簡單的指令checkout, 就可以輕鬆還原到"test1"的狀態了!
cat myFile.txt
givemepass
abcdefg
$ git checkout 9334a34c9946cd08abd3473f877bdcbbf637ea46
Note: checking out '9334a34c9946cd08abd3473f877bdcbbf637ea46'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 9334a34... test1
$ cat myFile.txt
givemepass
一開始確認一下, 檔案myFile.txt是不是我們修改過後的檔案,
嗯,沒錯! 那麼我們就利用checkout跳回原本test1的commit編號,
在確認一次myFile.txt, 沒錯! 變回原本的狀態了。


安裝流程請參考
如何使用版本控制(Git)-安裝流程
Branch請參考下一篇文章
如何使用版本控制(Git)-Branch