Git 的工作就是創建和保存你的項目的快照及與之后的快照進行對比。本章將對有關創建與提交你的項目的快照的命令作介紹。
用 git init 在目錄中創建新的 Git 倉庫。 你可以在任何時候、任何目錄中這么做,完全是本地化的。
在目錄中執行 git init,就可以創建一個 Git 倉庫了。比如我們創建 w3cschoolcc 項目:
$ mkdir w3cschoolcc $ cd w3cschoolcc $ git init Initialized empty Git repository in /www/w3cschoolcc/.git/ # 在 /www/w3cschoolcc/.git/ 目錄初始化空 Git 倉庫完畢。
現在你可以看到在你的項目目錄中有個 .git 的子目錄。 這就是你的 Git 倉庫了,所有有關你的此項目的快照數據都存放在這里。
ls -a . .. .git
使用 git clone 拷貝一個 Git 倉庫到本地,讓自己能夠查看該項目,或者進行修改。
如果你需要與他人合作一個項目,或者想要復制一個項目,看看代碼,你就可以克隆那個項目。 執行命令:
git clone [url]
[url] 為你想要復制的項目,就可以了。
例如我們克隆 Github 上的項目:
$ git clone git://github.com/schacon/simplegit.git Initialized empty Git repository in /private/tmp/simplegit/.git/ remote: Counting objects: 100, done. remote: Compressing objects: 100% (86/86), done. remote: Total 100 (delta 35), reused 0 (delta 0) Receiving objects: 100% (100/100), 9.51 KiB, done. Resolving deltas: 100% (35/35), done. $ 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 命令可將該文件添加到緩存,如我們添加以下兩個文件:
$ 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 .。
現在我們改個文件,再執行一下 git status:
$ vim README $ 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 以查看在你上次提交之后是否有修改。
我演示該命令的時候加了 -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 status 的結果的詳細信息。
git diff 命令顯示已寫入緩存與已修改但尚未寫入緩存的改動的區別。git diff 有兩個主要的應用場景。
在 hello.php 文件中輸入以下內容:
<?php echo 'www.w3cschool.cn'; ?>
$ git status -s A README AM hello.php $ git diff diff --git a/hello.php b/hello.php index e69de29..d1a9166 100644 --- a/hello.php +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo 'www.w3cschool.cn'; +?>
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..704cce7 --- /dev/null +++ b/README @@ -0,0 +1 @@ +w3cschool.cn diff --git a/hello.php b/hello.php new file mode 100644 index 0000000..d1a9166 --- /dev/null +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo 'www.w3cschool.cn'; +?>
使用 git add 命令將想要快照的內容寫入了緩存, 而執行 git commit 記錄緩存區的快照。
Git 為你的每一個提交都記錄你的名字與電子郵箱地址,所以第一步需要配置用戶名和郵箱地址。
$ git config --global user.name 'w3cschool' $ git config --global user.email w3c@w3cschool.cn
接下來我們寫入緩存,并提交對 hello.php 的所有改動。在首個例子中,我們使用 -m 選項以在命令行中提供提交注釋。
$ git add hello.php $ git status -s A README A hello.php $ git commit -m 'test comment from w3cschool.cn' [master (root-commit) 85fc7e7] test comment from w3cschool.cn 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)
以上輸出說明我們在最近一次提交之后,沒有做任何改動,是一個"干凈的工作目錄"。
如果你沒有設置 -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 ..." to unstage) # # modified: hello.php # ~ ~ ".git/COMMIT_EDITMSG" 9L, 257C如果你覺得 git add 提交緩存的流程太過繁瑣,Git 也允許你用 -a 選項跳過這一步。命令格式如下:
git commit -a
如:
$ git commit -am 'changes to hello file' [master 78b2670] changes to hello file 1 files changed, 2 insertions(+), 1 deletions(-)
git reset HEAD 命令用于取消緩存已緩存的內容。
這里我們有兩個最近提交之后又有所改動的文件。我們將兩個都緩存,并取消緩存其中一個。
$ 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.rb。
git rm 將文件從緩存區中移除。
如我們刪除 hello.php文件:
$ git rm hello.php rm 'hello.php' $ ls README
默認情況下,git rm file 會將文件從緩存區和你的硬盤中(工作目錄)刪除。 如果要在工作目錄中留著該文件,可以使用命令:
git rm --cached。
git mv 命令做得所有事情就是 git rm --cached, 重命名磁盤上的文件,然后再執行 git add 把新文件添加到緩存區。因此,雖然有 git mv 命令,但它有點多余 。