上一節就提到了倉庫的概念,其實初始化git後,git將本地空間分成兩部分,一部分是工作區(Working Directory),studygit目錄就是工作區存放我們自己的文件,另外一個就是版本庫(Repository)也稱爲倉庫,在工作區目錄下有一個.git的隱藏目錄,該目錄不屬於工作區,就是倉庫。
houenxun@studygit$ ls -al
total 0
drwxr-xr-x 3 houenxun staff 102 7 9 19:18 .
drwxr-xr-x+ 52 houenxun staff 1768 7 9 19:18 ..
drwxr-xr-x 10 houenxun staff 340 7 9 19:20 .git
通過git status 我們可以實時查看當前倉庫的狀態
houenxun@studygit$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
當前處於master分支,並且沒有任何東西需要提交,這是我們通過touch命令在工作區中創建一個文件,並再次查看倉庫信息
houenxun@studygit$ touch readme.txt
houenxun@studygit$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)
Untracked file 表示readme.txt是新增的文件,在倉庫中沒有版本信息。下面我們將readme.tex提交到本地倉庫中。
houenxun@studygit$ git add readme.txt
houenxun@studygit$ git commit -m "commit readme.txt"
[master (root-commit) 32a7706] commit readme.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
houenxun@studygit$
這裏可能有人會問題,提交本地倉庫,爲什麼先進行add操作然後再進行commit操作?
其實git的倉庫又進一步劃分出了一塊暫存區(stage),通過add命令只是將文件提交到了stage中,類似保存草稿。
現在查看一下git的狀態
houenxun@studygit$ git status
On branch master
nothing to commit, working directory clean
當前沒有任何文件需要提交。如果想查看提交記錄使用git log
houenxun@studygit$ git log
commit 32a7706df9f266522bdb223e8f4308cfccec01ba
Author: houenxun <[email protected]>
Date: Thu Jul 9 20:08:48 2015 +0800
commit readme.txt
其中32a7706df9f266522bdb223e8f4308cfccec01ba表示提交操作的唯一id
houenxun@studygit$ touch hello test test2
houenxun@studygit$ ls
hello readme.txt test test2
houenxun@studygit$ git add *
houenxun@studygit$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello
new file: test
new file: test2
下面我們一次創建多個文件,通過git add添加到暫存區中後看一下當前倉庫的狀態!另外需要說明的是git add 可以一次添加多個文件,即可以通過git file1 file2的形式,也可以通過通配符匹配多個文件!
houenxun@studygit$ git commit -m "hello test test2"
[master 75014c6] hello test test2
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello
create mode 100644 test
create mode 100644 test2
houenxun@studygit$ git log
commit 75014c60c33dca4873f80a80d3892cfef7b59340
Author: houenxun <[email protected]>
Date: Thu Jul 9 20:41:33 2015 +0800
hello test test2
commit 32a7706df9f266522bdb223e8f4308cfccec01ba
Author: houenxun <[email protected]>
Date: Thu Jul 9 20:08:48 2015 +0800
commit readme.txt
houenxun@studygit$
再次通過git commit 命令提交,查看提交記錄,是不是發現有了兩個版本!
這裏還有一個小功能,git log file 可以查看單個文件的提交記錄
houenxun@studygit$ git log readme.txt
commit 32a7706df9f266522bdb223e8f4308cfccec01ba
Author: houenxun <[email protected]>
Date: Thu Jul 9 20:08:48 2015 +0800
commit readme.txt
houenxun@studygit$
好了有關本地提交的操作就寫到這裏,但上面只是做了版本記錄的作用,其實並未真正展示版本控制系統的真正優勢,下節繼續!