Git 简单命令(一):git add 和 git commit

先贴一张大家都了解的Git 工作的理论模型图,仅包含了 git add 与 git commit 命令
Git 简单命令(一):git add 和 git commit_第1张图片

这张图涉及了一个底层命令概念理解,详细请查看:Git 命令分析(一):git add 与 git commit

Git 简单命令(一):git add 和 git commit_第2张图片

1. 主要内容:
  1. 按照图中红色箭头,进行实例操作
  2. 掌握:git add 与 git commit 命令执行后文件状态的变化

Git 简单命令(一):git add 和 git commit_第3张图片

2. 初始化

Git 简单命令(一):git add 和 git commit_第4张图片

# 初始化一个项目
$ git init GitDemo

# 切进去 GitDemo
$ cd GitDemo

# 查看状态
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
3. 实例操作:序号①—git add
  • 内容:创建新文件,并查看状态
# 创建新文件
$ touch README.md

# 查看状态
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        README.md
nothing added to commit but untracked files present (use "git add" to track)

新创建的文件 README.md 是未跟踪文件(Untracked files

  • 内容:文件从未跟踪到已暂存【待提交】的变化
# 将新文件添加到暂存区
$ git add README.md

# 查看状态
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   README.md

这时,README.md 文件处于“待提交”状态,即:已暂存

4. 实例操作:序号③—git commit
  • 内容:已暂存【待提交】到 已提交的状态改变
# 提交【暂存区的状态记录】
$ git commit
[master (root-commit) 3a3ae69] First Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

# 执行该命令,会启动默认的文本编辑器,笔者操作时使用的是vim 编辑器

# 查看状态
$ git status
On branch master
nothing to commit, working tree clean

这时,整个GitDemo 项目中的文件,都是已提交的状态

  • vim 编辑器简单使用:
  1. 执行后,就会打开vim 编辑器,处于页面查看状态
  2. 插入:i 键,然后输入提交信息
  3. 退出编辑:输入完后,按 ESC键,再按:退出编辑状态
  4. 保存:w 键,保存vim编辑器中编辑的信息
  5. 退出vim:q 键

一般,步骤 4、5是一块使用的,即:wq,保存退出

5. 实例操作:序号②—git add
  • 内容:已跟踪的文件,修改后,再暂存
# 修改之前,查看状态
$ git status
On branch master
nothing to commit, working tree clean

# 修改 README.md 文件内容【可以用文本编辑器】
使用文本编辑软件,键入:GitDemo

# 修改之后,查看状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   README.md

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

# 再次暂存
$ git add README.md

# 查看状态
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   README.md

修改之后,README.md 文件状态是:已修改(Modified)

6. 实例操作:序号③—git commit -m
  • 命令:git commit -m “提交信息”

该方式,在提交信息少时,比较方便,不需要专门启动 vim 编辑器键入提交信息

# 提交
$ git commit -m "Second Commit"
[master 4362fbe] Second Commit
 1 file changed, 1 insertion(+)

# 状态查看
$ git status
On branch master
nothing to commit, working tree clean
7. 实例操作:序号④—git commit -a -m
  • 命令:git commit -a -m “提交信息”

优点:已跟踪的文件,作出修改后,可以直接提交,操作方便

# 文本编辑器在 README.md 文件中,在内容之后,键入:123

# 修改之后,查看状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

# 直接提交
$ git commit -a -m "Third Commit"
[master f7fcfd3] Third Commit
 1 file changed, 1 insertion(+), 1 deletion(-)

# 查看状态
$ git status
On branch master
nothing to commit, working tree clean

参考文献:

Pro Git 网址:https://git-scm.com/book/zh/v2
如若想深入探究Git,请阅读该书(免费,开源)

本文属于原创,转载请注明出处

你可能感兴趣的:(VCS,#,Git)