一、基础命令

1、git status:

功能:查看工作区、暂存区状态

$ git status

窗口反馈内容:

On branch master

//该工作区在master分支上

No commits yet
//本地库无内容。(commits的内容是要提交到本地库里的)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Git-GitHub.pdf
//工作区

nothing added to commit but untracked files present (use "git add" to track)
//暂存区

2、git add:

将工作区文件添加到暂存区
(跟踪文件)

$ git add Git-GitHub.pdf
$ git status

窗口显示:

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   Git-GitHub.pdf

暂存区有了新文件。不过提示里的“git rm —cached …” to unstage”是什么意思?

3、git rm —cached

功能:将文件从缓冲区删除

$ git rm --cached Git-GitHub.pdf
$ git status

窗口显示:

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Git-GitHub.pdf

nothing added to commit but untracked files present (use "git add" to track)

文件已被从缓存区删除。

4、git commit

$ git commit Git-GitHub.pdf

输入这条命令,弹出了
如下窗口

现在处于vim编辑器中。
vim编辑器的简单使用:
按“i”键进入Insert模式,编辑文本。
(注意:#开始的文字会被屏蔽)
编辑结束后,按Esc打开输入栏,再输入”:wq”保存。

$ git add Git-GitHub.pdf
$ git commit Git-GitHub.pdf

窗口显示:

[master (root-commit) f228a51] My first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Git-GitHub.pdf

分析:

  • [master (root-commit) f228a51] My first commit
  • /“root-commit”:由于这是该库的第一次提交,故被分配为root-commit,“My first commit”是commit时所标注的信息/
  • 1 file changed, 0 insertions(+), 0 deletions(-)
  • /一个文件被修改,新增0行,减少0行。由于这是pdf文件,压根不存在行/

Tip: git commit -m “<提交备注>” [<文件名>]命令可以省去编辑备注的操作

二、新建和修改的区别

新建文件文件并提交到本地库:

$ vim hello.txt
$ git add hello.txt
$ git commit hello.txt

窗口显示:

warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master 36ac922] 测试一下新建文件的提交:
 1 file changed, 2 insertions(+)
 create mode 100644 hello.txt

hello.txt内的内容:

试一试新建文件
一共有两行

再修改文件:

vim hello.txt

hello.txt内的内容:

a
b
c

再次查看状态:


$ git status

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

注意最后一行:(use “git add” and/or “git commit -a”)
修改后的文件也可以直接使用git commit -a提交到缓存区。

$ git add hello.txt
$ git commit -m "哈哈" hello.txt

窗口显示:

warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master 0e1f499] 哈哈
 1 file changed, 3 insertions(+), 2 deletions(-)

注意:这次的提交是:[master 0e1f499]。而不是root-commit。因为这不是该工作区的第一次提交