【Git】二、文件的提交与查看

提要

//添加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
原文地址:https://www.cnblogs.com/shanelau/p/7101804.html