提要
//添加git跟蹤文件,stage操作
$git add readme.txt
//提交到本地分支
$git commit -m xxx
//查看當前git工作狀態,可以看到未跟蹤文件,已跟蹤未stage文件,已stage可commit文件
$git status
//查看某文件的差異,只能查看
$ git diff readme.txt
一、提交一個文件 commit
在剛剛創建好的倉庫文件夾里面創建一個文件readme.txt
commit操作一共分2步,類似於圖形操作里面的stage、commit
$git add readme.txt
$git commit -m xxx
注:git的add可以add多個文件,或多次add;
add的作用就是開始對這個文件的修改進行跟蹤,沒有add過的新建文件的修改是不會被記錄的;
commit后面的-m表示本次提交的說明,類似於圖形界面里面的comment
二、查看狀態與修改
用指令可以查看當前修改文件、stage文件、提交文件的狀態;並可以查看某一個文件的差異
修改readme.txt,再新建一個testgit.txt文件,使用指令查看文件狀態
$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)
testgit.txt
no changes added to commit (use "git add" and/or "git commit -a")
注:這段告訴我們:在master分支,跟蹤到的沒有stage的文件有readme.txt,為跟蹤到的文件testgit.txt
使用diff指令查看文件差異
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index d3527b2..f9fd80e 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1 @@
-this is a read me file
\ No newline at end of file
+this a read me file
\ No newline at end of file
然后我們把testgit.txt add一下,再查看status
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: testgit.txt
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
注:這段告訴我們:master可以提交的修改是一個新增的文件testgit.txt,另一個readme.txt有修改但還不能commit,因為沒有add
將readme.txt add,並commit
$ git add readme.txt
$ git commit -m "test commit,2 files is commited"
[master a50498c] test commit,2 files is commited
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 testgit.txt
這時所有的修改都提交了,現在再查看下狀態
$ git status
On branch master
nothing to commit, working tree clean