1. 程式人生 > >Git的學習與使用(六)——Git 基本操作

Git的學習與使用(六)——Git 基本操作

Git 基本操作

Git 的工作就是建立和儲存你專案的快照及與之後的快照進行對比。本章將對有關建立與提交你的專案快照的命令作介紹。


獲取與建立專案命令

git init

用 git init 在目錄中建立新的 Git 倉庫。 你可以在任何時候、任何目錄中這麼做,完全是本地化的。

在目錄中執行 git init,就可以建立一個 Git 倉庫了。比如我們建立 test 專案:

$ mkdir test
$ cd test/
$ git init
Initialized empty Git repository in /Users/project/www/test/.git/
# 在 /www/test/.git/ 目錄初始化空 Git 倉庫完畢。

現在你可以看到在你的專案中生成了 .git 這個子目錄。 這就是你的 Git 倉庫了,所有有關你的此專案的快照資料都存放在這裡。

ls -a
.    ..    .git

git clone

使用 git clone 拷貝一個 Git 倉庫到本地,讓自己能夠檢視該專案,或者進行修改。

如果你需要與他人合作一個專案,或者想要複製一個專案,看看程式碼,你就可以克隆那個專案。 執行命令:

 git clone [url]

[url] 為你想要複製的專案,就可以了。

例如我們克隆 Github 上的專案:

$ git clone [email protected]:schacon/simplegit.git
Cloning into 'simplegit'...
remote: Counting objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.
Checking connectivity... done.

克隆完成後,在當前目錄下會生成一個 simplegit 目錄:

$ cd simplegit/
$ ls
README   Rakefile lib

上述操作將複製該專案的全部記錄。

$ ls -a
.        ..       .git     README   Rakefile lib
$ cd .git
$ ls
HEAD        description info        packed-refs
branches    hooks       logs        refs
config      index       objects

預設情況下,Git 會按照你提供的 URL 所指示的專案的名稱建立你的本地專案目錄。 通常就是該 URL 最後一個 / 之後的專案名稱。如果你想要一個不一樣的名字, 你可以在該命令後加上你想要的名稱。


基本快照

Git 的工作就是建立和儲存你的專案的快照及與之後的快照進行對比。本章將對有關建立與提交你的專案的快照的命令作介紹。

git add

git add 命令可將該檔案新增到快取,如我們新增以下兩個檔案:

$ touch README
$ touch hello.php
$ ls
README        hello.php
$ git status -s?? README
?? hello.php
$

git status 命令用於檢視專案的當前狀態。

接下來我們執行 git add 命令來新增檔案:

$ git add README hello.php

現在我們再執行 git status,就可以看到這兩個檔案已經加上去了。

$ git status -s
A  README
A  hello.php
$

新專案中,新增所有檔案很普遍,我們可以使用 git add . 命令來添加當前專案的所有檔案。

現在我們修改 README 檔案:

$ vim README

在 README 新增以下內容:# Runoob Git 測試,然後儲存退出。

再執行一下 git status:

$ git status -s
AM README
A  hello.php

"AM" 狀態的意思是,這個檔案在我們將它新增到快取之後又有改動。改動後我們再執行 git add 命令將其新增到快取中:

$ git add .
$ git status -s
A  README
A  hello.php

當你要將你的修改包含在即將提交的快照裡的時候,需要執行 git add。

git status

git status 以檢視在你上次提交之後是否有修改。

我演示該命令的時候加了 -s 引數,以獲得簡短的結果輸出。如果沒加該引數會詳細輸出內容:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)   
    new file:   README
    new file:   hello.php

git diff

執行 git diff 來檢視執行 git status 的結果的詳細資訊。

git diff 命令顯示已寫入快取與已修改但尚未寫入快取的改動的區別。git diff 有兩個主要的應用場景。

  • 尚未快取的改動:git diff

  • 檢視已快取的改動: git diff --cached

  • 檢視已快取的與未快取的所有改動:git diff HEAD

  • 顯示摘要而非整個 diff:git diff --stat

在 hello.php 檔案中輸入以下內容:

<?php
echo '歡迎來到我的世界';
?>
$ git status -s
A  README
AM hello.php
$ git diff
diff --git a/hello.php b/hello.php
index e69de29..69b5711 100644
--- a/hello.php
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo '歡迎來到我的世界';
+?>

git status 顯示你上次提交更新後的更改或者寫入快取的改動, 而 git diff 一行一行地顯示這些改動具體是啥。

接下來我們來檢視下 git diff --cached 的執行效果:

$ git add hello.php 
$ git status -s
A  README
A  hello.php
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..8f87495
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+# Runoob Git 測試
diff --git a/hello.php b/hello.php
new file mode 100644
index 0000000..69b5711
--- /dev/null
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo '歡迎來到我的世界';
+?>

git commit

使用 git add 命令將想要快照的內容寫入快取區, 而執行 git commit 將快取區內容新增到倉庫中。

Git 為你的每一個提交都記錄你的名字與電子郵箱地址,所以第一步需要配置使用者名稱和郵箱地址。

$ git config --global user.name 'test'
$ git config --global user.email [email protected]

接下來我們寫入快取,並提交對 hello.php 的所有改動。在首個例子中,我們使用 -m 選項以在命令列中提供提交註釋。

$ git add hello.php
$ git status -s
A  README
A  hello.php
$ $ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php

現在我們已經記錄了快照。如果我們再執行 git status:

$ git status
# On branch master
nothing to commit (working directory clean)

以上輸出說明我們在最近一次提交之後,沒有做任何改動,是一個"working directory clean:乾淨的工作目錄"。

如果你沒有設定 -m 選項,Git 會嘗試為你開啟一個編輯器以填寫提交資訊。 如果 Git 在你對它的配置中找不到相關資訊,預設會開啟 vim。螢幕會像這樣:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

如果你覺得 git add 提交快取的流程太過繁瑣,Git 也允許你用 -a 選項跳過這一步。命令格式如下:

git commit -a

我們先修改 hello.php 檔案為以下內容:

<?php
echo '歡迎來到我的世界';
echo '歡迎來到我的世界'; 
?>

再執行以下命令:

git commit -am '修改 hello.php 檔案'
[master 71ee2cb] 修改 hello.php 檔案
 1 file changed, 1 insertion(+)

git reset HEAD

git reset HEAD 命令用於取消已快取的內容。

我們先改動檔案 README 檔案,內容如下:

# Runoob Git 測試
# 我的世界

hello.php 檔案修改為:

<?php
echo '歡迎來到我的世界'; 
echo '歡迎來到我的世界'; 
echo '歡迎來到我的世界'; 
?>

現在兩個檔案修改後,都提交到了快取區,我們現在要取消其中一個的快取,操作如下:

$ git status -s
 M README
 M hello.php
$ git add .
$ git status -s
M  README
M  hello.pp
$ git reset HEAD hello.php 
Unstaged changes after reset:M    hello.php
$ git status -s
M  README
 M hello.php

現在你執行 git commit,只會將 README 檔案的改動提交,而 hello.php 是沒有的。

$ git commit -m '修改'
[master f50cfda] 修改
 1 file changed, 1 insertion(+)
$ git status -s
 M hello.php

可以看到 hello.php 檔案的修改並未提交。

這時我們可以使用以下命令將 hello.php 的修改提交:

$ git commit -am '修改 hello.php 檔案'
[master 760f74d] 修改 hello.php 檔案
 1 file changed, 1 insertion(+)
$ git statusOn branch master
nothing to commit, working directory clean

簡而言之,執行 git reset HEAD 以取消之前 git add 新增,但不希望包含在下一提交快照中的快取。

git rm

如果只是簡單地從工作目錄中手工刪除檔案,執行 git status 時就會在 Changes not staged for commit 的提示。

要從 Git 中移除某個檔案,就必須要從已跟蹤檔案清單中移除,然後提交。可以用以下命令完成此項工作

git rm <file>

如果刪除之前修改過並且已經放到暫存區域的話,則必須要用強制刪除選項 -f

git rm -f <file>

如果把檔案從暫存區域移除,但仍然希望保留在當前工作目錄中,換句話說,僅是從跟蹤清單中刪除,使用 --cached 選項即可

git rm --cached <file>

如我們刪除 hello.php檔案:

$ git rm hello.php 
rm 'hello.php'
$ ls
README

不從工作區中刪除檔案:

$ git rm --cached README 
rm 'README'
$ ls
README

可以遞迴刪除,即如果後面跟的是一個目錄做為引數,則會遞迴刪除整個目錄中的所有子目錄和檔案:

git rm –r *

進入某個目錄中,執