先贴一张大家都了解的Git 工作的理论模型图,仅包含了 git add 与 git commit 命令
这张图涉及了一个底层命令概念理解,详细请查看:Git 命令分析(一):git add 与 git commit
- 按照图中红色箭头,进行实例操作
- 掌握:git add 与 git commit 命令执行后文件状态的变化
# 初始化一个项目
$ 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)
# 创建新文件
$ 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 文件处于“待提交”状态,即:已暂存
# 提交【暂存区的状态记录】
$ 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 编辑器简单使用:
- 执行后,就会打开vim 编辑器,处于页面查看状态
- 插入:i 键,然后输入提交信息
- 退出编辑:输入完后,按
ESC
键,再按:
退出编辑状态- 保存:w 键,保存vim编辑器中编辑的信息
- 退出vim:q 键
一般,步骤 4、5是一块使用的,即:wq,保存退出
# 修改之前,查看状态
$ 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)
该方式,在提交信息少时,比较方便,不需要专门启动 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
优点:已跟踪的文件,作出修改后,可以直接提交,操作方便
# 文本编辑器在 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,请阅读该书(免费,开源)
本文属于原创,转载请注明出处