Git 的工作就是創(chuàng)建和保存你項(xiàng)目的快照及與之后的快照進(jìn)行對(duì)比。本章將對(duì)有關(guān)創(chuàng)建與提交你的項(xiàng)目快照的命令作介紹。
用 git init 在目錄中創(chuàng)建新的 Git 倉(cāng)庫(kù)。 你可以在任何時(shí)候、任何目錄中這么做,完全是本地化的。
在目錄中執(zhí)行 git init,就可以創(chuàng)建一個(gè) Git 倉(cāng)庫(kù)了。比如我們創(chuàng)建bjpowernode項(xiàng)目:
$ mkdir bjpowernode
$ cd bjpowernode/
$ git init
Initialized empty Git repository in /Users/tianqixin/www/bjpowernode/.git/
# 在 /www/bjpowernode/.git/ 目錄初始化空 Git 倉(cāng)庫(kù)完畢。
現(xiàn)在你可以看到在你的項(xiàng)目中生成了 .git 這個(gè)子目錄。 這就是你的 Git 倉(cāng)庫(kù)了,所有有關(guān)你的此項(xiàng)目的快照數(shù)據(jù)都存放在這里。
ls -a
. .. .git
⒉ git clone
使用 git clone 拷貝一個(gè) Git 倉(cāng)庫(kù)到本地,讓自己能夠查看該項(xiàng)目,或者進(jìn)行修改。
如果你需要與他人合作一個(gè)項(xiàng)目,或者想要復(fù)制一個(gè)項(xiàng)目,看看代碼,你就可以克隆那個(gè)項(xiàng)目。 執(zhí)行命令:
git clone [url]
[url] 為你想要復(fù)制的項(xiàng)目,就可以了。
例如我們克隆 Github 上的項(xiàng)目:
$ git clone git@github.com: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.
克隆完成后,在當(dāng)前目錄下會(huì)生成一個(gè) simplegit 目錄:
$ cd simplegit/
$ ls
README Rakefile lib
上述操作將復(fù)制該項(xiàng)目的全部記錄。
$ ls -a
. .. .git README Rakefile lib
$ cd .git
$ ls
HEAD description info packed-refs
branches hooks logs refs
config index objects
默認(rèn)情況下,Git 會(huì)按照你提供的 URL 所指示的項(xiàng)目的名稱創(chuàng)建你的本地項(xiàng)目目錄。 通常就是該 URL 最后一個(gè) / 之后的項(xiàng)目名稱。如果你想要一個(gè)不一樣的名字, 你可以在該命令后加上你想要的名稱。
Git 的工作就是創(chuàng)建和保存你的項(xiàng)目的快照及與之后的快照進(jìn)行對(duì)比。本章將對(duì)有關(guān)創(chuàng)建與提交你的項(xiàng)目的快照的命令作介紹。
git add 命令可將該文件添加到緩存,如我們添加以下兩個(gè)文件:
$ touch README
$ touch hello.php
$ ls
README hello.php
$ git status -s
?? README
?? hello.php
$
git status 命令用于查看項(xiàng)目的當(dāng)前狀態(tài)。
接下來(lái)我們執(zhí)行 git add 命令來(lái)添加文件:
$ git add README hello.php
現(xiàn)在我們?cè)賵?zhí)行 git status,就可以看到這兩個(gè)文件已經(jīng)加上去了。
$ git status -s
A README
A hello.php
$
新項(xiàng)目中,添加所有文件很普遍,我們可以使用 git add . 命令來(lái)添加當(dāng)前項(xiàng)目的所有文件。
現(xiàn)在我們修改 README 文件:
$ vim README
在 README 添加以下內(nèi)容:# bjpowernode Git 測(cè)試,然后保存退出。
再執(zhí)行一下 git status:
$ git status -s
AM README
A hello.php
"AM" 狀態(tài)的意思是,這個(gè)文件在我們將它添加到緩存之后又有改動(dòng)。改動(dòng)后我們?cè)賵?zhí)行 git add 命令將其添加到緩存中:
$ git add .
$ git status -s
A README
A hello.php
當(dāng)你要將你的修改包含在即將提交的快照里的時(shí)候,需要執(zhí)行 git add。
git status 以查看在你上次提交之后是否有修改。
我演示該命令的時(shí)候加了 -s 參數(shù),以獲得簡(jiǎn)短的結(jié)果輸出。如果沒(méi)加該參數(shù)會(huì)詳細(xì)輸出內(nèi)容:
$ 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
執(zhí)行 git diff 來(lái)查看執(zhí)行 git status 的結(jié)果的詳細(xì)信息。
git diff 命令顯示已寫(xiě)入緩存與已修改但尚未寫(xiě)入緩存的改動(dòng)的區(qū)別。git diff 有兩個(gè)主要的應(yīng)用場(chǎng)景。
① 尚未緩存的改動(dòng):git diff
② 查看已緩存的改動(dòng): git diff --cached
③ 查看已緩存的與未緩存的所有改動(dòng):git diff HEAD
④ 顯示摘要而非整個(gè) diff:git diff --stat
在 hello.php 文件中輸入以下內(nèi)容:
<?php
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
?>
$ 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 '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
+?>
git status 顯示你上次提交更新后的更改或者寫(xiě)入緩存的改動(dòng), 而 git diff 一行一行地顯示這些改動(dòng)具體是啥。
接下來(lái)我們來(lái)查看下 git diff --cached 的執(zhí)行效果:
$ 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 @@
+# bjpowernode Git 測(cè)試
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 '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
+?>
⒋ git commit
使用 git add 命令將想要快照的內(nèi)容寫(xiě)入緩存區(qū), 而執(zhí)行 git commit 將緩存區(qū)內(nèi)容添加到倉(cāng)庫(kù)中。
Git 為你的每一個(gè)提交都記錄你的名字與電子郵箱地址,所以第一步需要配置用戶名和郵箱地址。
$ git config --global user.name 'bjpowernode'
$ git config --global user.email test@bjpowernode.com
接下來(lái)我們寫(xiě)入緩存,并提交對(duì) hello.php 的所有改動(dòng)。在首個(gè)例子中,我們使用 -m 選項(xiàng)以在命令行中提供提交注釋。
$ 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
現(xiàn)在我們已經(jīng)記錄了快照。如果我們?cè)賵?zhí)行 git status:
$ git status
# On branch master
nothing to commit (working directory clean)
以上輸出說(shuō)明我們?cè)谧罱淮翁峤恢螅瑳](méi)有做任何改動(dòng),是一個(gè)"working directory clean:干凈的工作目錄"。
如果你沒(méi)有設(shè)置 -m 選項(xiàng),Git 會(huì)嘗試為你打開(kāi)一個(gè)編輯器以填寫(xiě)提交信息。 如果 Git 在你對(duì)它的配置中找不到相關(guān)信息,默認(rèn)會(huì)打開(kāi) vim。屏幕會(huì)像這樣:
# 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
如果你覺(jué)得 git add 提交緩存的流程太過(guò)繁瑣,Git 也允許你用 -a 選項(xiàng)跳過(guò)這一步。命令格式如下:
git commit -a
我們先修改 hello.php 文件為以下內(nèi)容:
<?php
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
?>
再執(zhí)行以下命令:
git commit -am '修改 hello.php 文件'
[master 71ee2cb] 修改 hello.php 文件
1 file changed, 1 insertion(+)
⒌ git reset HEAD
git reset HEAD 命令用于取消已緩存的內(nèi)容。
我們先改動(dòng)文件 README 文件,內(nèi)容如下:
# bjpowernode Git 測(cè)試
# 動(dòng)力節(jié)點(diǎn)
hello.php 文件修改為:
<?php
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
echo '動(dòng)力節(jié)點(diǎn):www.dabaquan.cn';
?>
現(xiàn)在兩個(gè)文件修改后,都提交到了緩存區(qū),我們現(xiàn)在要取消其中一個(gè)的緩存,操作如下:
$ git status -s
M README
M hello.php
$ git add .
$ git status -s
M README
M hello.php
$ git reset HEAD hello.php
Unstaged changes after reset:
M hello.php
$ git status -s
M README
M hello.php
現(xiàn)在你執(zhí)行 git commit,只會(huì)將 README 文件的改動(dòng)提交,而 hello.php 是沒(méi)有的。
$ git commit -m '修改'
[master f50cfda] 修改
1 file changed, 1 insertion(+)
$ git status -s
M hello.php
可以看到 hello.php 文件的修改并未提交。
這時(shí)我們可以使用以下命令將 hello.php 的修改提交:
$ git commit -am '修改 hello.php 文件'
[master 760f74d] 修改 hello.php 文件
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean
簡(jiǎn)而言之,執(zhí)行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的緩存。
如果只是簡(jiǎn)單地從工作目錄中手工刪除文件,運(yùn)行 git status 時(shí)就會(huì)在 Changes not staged for commit 的提示。
要從 Git 中移除某個(gè)文件,就必須要從已跟蹤文件清單中移除,然后提交。可以用以下命令完成此項(xiàng)工作
git rm <file>
如果刪除之前修改過(guò)并且已經(jīng)放到暫存區(qū)域的話,則必須要用強(qiáng)制刪除選項(xiàng) -f
git rm -f <file>
如果把文件從暫存區(qū)域移除,但仍然希望保留在當(dāng)前工作目錄中,換句話說(shuō),僅是從跟蹤清單中刪除,使用 --cached 選項(xiàng)即可
git rm --cached <file>
如我們刪除 hello.php文件:
$ git rm hello.php
rm 'hello.php'
$ ls
README
不從工作區(qū)中刪除文件:
$ git rm --cached README
rm 'README'
$ ls
README
可以遞歸刪除,即如果后面跟的是一個(gè)目錄做為參數(shù),則會(huì)遞歸刪除整個(gè)目錄中的所有子目錄和文件:
git rm –r *
進(jìn)入某個(gè)目錄中,執(zhí)行此語(yǔ)句,會(huì)刪除該目錄下的所有文件和子目錄。
git mv 命令用于移動(dòng)或重命名一個(gè)文件、目錄、軟連接。
我們先把剛移除的 README 添加回來(lái):
$ git add README
然后對(duì)其重名:
$ git mv README README.md
$ ls
README.md