git commit
使用 git add 命令將想要快照的內容寫入緩存區, 而執行 git commit 將緩存區內容添加到本地倉庫中。
Git 為你的每一個提交都記錄你的名字與電子郵箱地址,所以第一步需要配置用戶名和郵箱地址。
注意,這里配置的用戶名與郵箱,只是為了標識用戶,不做任何驗證作用。
$ git config --global user.name 'runoob' $ git config --global user.email test@runoob.com
接下來我們寫入緩存,並提交對 hello.php 的所有改動。在首個例子中,我們使用 -m 選項以在命令行中提供提交注釋。
$ 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
現在我們已經記錄了快照。如果我們再執行 git status:
$ git status
# On branch master
nothing to commit (working directory clean)
以上輸出說明我們在最近一次提交之后,沒有做任何改動,是一個"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 <file>..." to unstage) # # modified: hello.php # ~ ~ ".git/COMMIT_EDITMSG" 9L, 257C
如果你覺得 git add 提交緩存的流程太過繁瑣,Git 也允許你用 -a 選項跳過這一步。命令格式如下:
git commit -a
我們先修改 hello.php 文件為以下內容:
<?php echo '菜鳥教程:www.runoob.com'; echo '菜鳥教程:www.runoob.com'; ?>
再執行以下命令:
git commit -am '修改 hello.php 文件' [master 71ee2cb] 修改 hello.php 文件 1 file changed, 1 insertion(+)