git 入门教程之版本管理

版本管理

背景

在上一节中咱们已经成功建立版本库而且已经添加test.txt等文件,这一节咱们继续讲解如何进行版本控制.git

首先咱们先查看test.txt 文件有什么内容吧!bash

# 查看文件内容
$ cat test.txt
git test
git init
git diff 
$
复制代码

接下来模拟正常工做,接着输入一下内容:ui

# 追加新内容到 test.txt 文件
echo "understand how git control version" >> test.txt

# 查看当前文件内容
$ cat test.txt
git test
git init
git diff
understand how git control version
$ 
复制代码

紧接着运行 git status 看一下输出结果:spa

# 查看文件状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   test.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:   test.txt

$ 
复制代码

从上述 git status 命令输出的结果能够看出,test.txt 已经被修改但还没提交,可是具体发生了什么变化却没能告诉咱们,若是可以告诉咱们具体修改细节那就行了!版本控制

运行**git diff**命令能够实现上述需求code

$ git diff
diff --git a/test.txt b/test.txt
index 729112f..989ce33 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 git test
 git init
 git diff
+understand how git control version
$ 
复制代码

git diff 命令即查看差别(difference),从输出结果能够看出咱们在最后一行新增了understand how git control version 文字.rem

经过git status 知道文件发生了改动,git diff 让咱们看到了改动的细节,如今咱们提交到版本库就放心多了,还记得上节课如何添加版本库的命令吗?string

分两步操做: git add <file>git commit -m <remark>it

第一步: git add <file>io

$ git add test.txt
$ 
复制代码

等一下,在执行 git commit 命令以前,咱们再运行 git status 命令查看一下当前仓库状态:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   test.txt

$ 
复制代码

此时 git status 命令告诉咱们 test.txt 文件已被修改等待提交,好了,那么接着第二步的commit吧!

第二步: git commit -m <remark>

# 提交到版本库并添加备注
$ git commit -m "add understand how git control version"
[master 36f234a] add understand how git control version
 1 file changed, 2 insertions(+)
$ 
复制代码

提交后,咱们此时再次运行git status 命令查看当前仓库状态:

$ git status
On branch master
nothing to commit, working tree clean
$ 
复制代码

输出结果显示没有须要提价的改动,工做目录是干净的.

小结

  • 查看工做区状态 git status
  • 比较修改差别 git diff
相关文章
相关标签/搜索