1. 文件狀態
一般倉庫中的文件可能存在於這三種狀態:
1)Untracked files → 文件未被跟蹤;
2)Changes to be committed → 文件已暫存,這是下次提交的內容;
3) Changes bu not updated → 文件被修改,但並沒有添加到暫存區。如果 commit 時沒有帶 -a 選項,這個狀態下的文件不會被提交。
$git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: file2 # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file3
2. 提交
git 提交的命令爲:git commit 。
2.1 git commit 與 git commit -a
git commit 提交的是暫存區裏面的內容,也就是 Changes to be committed 中的文件。
$git commit [master 5b61c29] run git commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file2 $git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file3
git commit -a 除了將暫存區裏的文件提交外,還提交 Changes bu not updated 中的文件。
$git commit -a [master bd77524] run git commit -a 1 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 file2 $git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file3
2.2 添加提交信息
如果直接運行 git commit (-a) 則會默認使用 vi 添加描述。也可以使用
git config --global core.editor
命令更改爲你喜歡的編輯器。還有一個方法就是使用 -m 選項直接添加提交信息。
$git commit -a -m "commit info"
3. 修改/取消
有時候我們會發現有幾個文件漏了提交或者想修改一下提交信息,又或者忘記使用 -a 選項導致一些文件沒有被提交,我們希望對上一次提交進行修改,或者說取消上一次提交,這時候我們需要使用 --amend 選項。
$git commit --amend
可以對上一次提交進行修改,比如我們發現漏了 file3 沒有提交,我們可以運行一下操作:
$git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file3 nothing added to commit but untracked files present (use "git add" to track) $git add file3 $git commit --amend [master 671f5cc] commit --amend, add file3 1 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 file2 create mode 100644 file3 $git status # On branch master nothing to commit (working directory clean)
又或者我們發現在提交時忘記使用 -a 選項,導致 Changes bu not updated 中的內容沒有被提交,我們可以使用:
$git commit --amend -a