一、創建初始版本庫:
執行 git init,將當前目錄轉化爲Git版本庫。
$ git init
Initialized empty Git repository in D:/Git/.git/
二、將文件添加到版本庫中:
使用 git add file 將file 添加到版本庫中
$ echo 'hello world' > hello.txt
$ git add hello.txt
運行 git status 命令,顯示中間狀態的 hello.txt 。
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
一條完全限定的 git commit 命令必須提供日誌消息和作者。
$ git commit -m "Initial contents of hello"
[master (root-commit) 8d290ef] Initial contents of hello
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
可以在命令中提供一條日誌消息,但更典型的做法是在交互式編輯器會話期間創建消息。爲了在 git commit 期間讓 Git 打開你喜歡的編輯器,要設置你的 GIT_EDITOR環境變量。
$ export GIT_EDITOR=vim
把新文件提交到版本庫中後, git status 命令顯示沒有突出的、暫存的變更需要文件。
三、配置提交作者:
可以用 git config 命令在配置文件中保存你的身份.
$ git config user.name "Ang"
$ git config user.emal "[email protected]"
也可以使用 GIT_AUEHOR_NAME 和 GIT_AUTHOR_EMAIL 環境變量來告訴Git你的姓名和email地址。這些變量一旦設置就會覆蓋所有的配置設置。
四、再次提及:
來提交一次對 hello.txt 文件的修改。
$ cat hello.txt
hello world
hi
$ git commit hello.txt
在這裏不需要執行 git add hello.txt,因爲這個文件已經存在版本庫裏了。
五、查看提交:
git log 命令會產生版本庫裏一系列單獨提交的歷史。
$ git log
commit 714bc280a9a237a986021357700b2af3226fe663
Author: Ang <[email protected]>
Date: Wed Aug 2 15:42:56 2017 +0800
my first revision
commit f857d79d4d83ed4906895f3f3597d86c765aa22c
Author: Ang <[email protected]>
Date: Wed Aug 2 15:41:06 2017 +0800
Initial contents of hello
爲了查看特定提交的更加詳細的信息,可以使用 git show 命令帶一個提交碼。
$ git show 714bc280a9a237a986021357700b2af3226fe663
commit 714bc280a9a237a986021357700b2af3226fe663
Author: Ang <[email protected]>
Date: Wed Aug 2 15:42:56 2017 +0800
my first revision
diff --git a/hello.txt b/hello.txt
index 3b18e51..e30a896 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
hello world
+
+hi
\ No newline at end of file
如果在執行 git show 命令的時候沒有顯示指定提交碼,它將只顯示最近一次提交的詳細信息。
另一種查看方式是使用 show-branch,提供當前開發分支簡介的單行摘要。
$ git show-branch --more=10
[master] my first revision
[master^] Initial contents of hello
參數“- -more=10”表示額外的10個版本,此處只有兩個版本。
六、查看提交差異:
使用兩個提交的全ID名並運行 git diff。
$ git diff f857d79d4d83ed4906895f3f3597d86c765aa22c \
> 714bc280a9a237a986021357700b2af322
diff --git a/hello.txt b/hello.txt
index 3b18e51..e30a896 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,3 @@
hello world
+
+hi
\ No newline at end of file
七、版本庫內文件的刪除和重命名:
使用 git rm 命令從版本庫中刪除一個文件。接着使用 git commit 在版本庫裏實現這個變更。
$ ls
hello.txt
$ git rm hello.txt
rm 'hello.txt'
$ git commit -m "remove"
[master 8734a78] remove
1 file changed, 3 deletions(-)
delete mode 100644 hello.txt
可以通過 git rm 和 git add 組合命令來間接爲一個文件重命名
$ mv foo.txt bar.txt
$ git rm foo.txt
rm 'foo.txt'
$ git add bar.txt
也可以直接使用 git mv 命令。
$ git mv foo.txt bar.txt
在任意一種情況下,暫存的變更必須隨後進行提交。
$ git commit -m "moved foo to bar"
[master a1e7a27] moved foo to bar
1 file changed, 0 insertions(+), 0 deletions(-)
rename foo.txt => bar.txt (100%)
八、創建版本庫副本:
之前在 D:/Git/git 目錄中建立了一個初始版本庫,就可以通過 git clone 命令創建一個完整的副本。
在 Git 目錄裏建立一個副本,並命名爲 my_website。
$ cd ..
$ git clone git my_website
Cloning into 'my_website'...
done.