工作區
電腦中的目錄,某個文件夾就是工作區。
版本庫
工作區有一個隱藏目錄.git,這個不算工作區,而是Git的版本庫。
Git的版本庫裏存了很多東西,其中最重要的就是稱爲stage(或者叫index)的暫存區,還有Git爲我們自動創建的第一個分支master,以及指向master的一個指針叫HEAD
文件往Git版本庫裏添加的時候,是分兩步執行的:
第一步是用git add把文件添加進去,實際上就是把工作區的文件修改添加到暫存區;
第二步是用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支。
因爲我們創建Git版本庫時,Git自動爲我們創建了唯一一個master分支,所以,現在,git commit就是往master分支上提交更改。
可以簡單理解爲,需要提交的文件修改通通放到暫存區,然後,一次性提交暫存區的所有修改。
實際操作加深理解
首先對工作區中的readme.txt文件修改,再新建一個新的LICENSE.txt文件,先用git status查看當前狀態:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
Git告訴我們readme.txt被修改了,LICENSE.txt沒有被添加過,狀態是untracked。
使用 git add將readme.txt和LICENSE.txt添加後,使用git status查看:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: readme.txt
此時的暫存區狀態:
git add命令是把工作區中要提交的所有修改放到暫存區(stage),然後執行git commit介意一次性將暫存區中的修改提交到分支master中。commit後,此時的status是:工作區是乾淨的(git status是查看工作區中的狀態)
$ git status
On branch master
nothing to commit, working tree clean
此時的版本庫: