Git学习
创建版本库
打开GitBash后,在某目录下执行下面的命令:
$ git init
添加文件到版本库
$ git add a.txt
$ git commit -m "add file a.txt"
其中,-m
参数表示后面的add file a.txt
为本次提交的说明。
按顺序执行上面的命令可以成功将文件a.txt
提交到版本库。
版本库状态
$ git status
假设在上一步提交a.txt
之后,没有再做其他修改,那么在执行上面的命令后,命令行工具将提示下面的信息:
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit, working directory clean
如果对a.txt
做了修改,但并没有执行$ git add a.txt
命令,这是版本库的状态为:
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# 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: a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
如果修改a.txt
后执行了$ git add a.txt
,但是没有执行$ git commit
命令,此时版本库状态为:
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
#
查看修改内容
$ git diff a.txt
这里有几个概念要注意:
名称 | |
---|---|
工作区 | 可以理解为建立版本库的目录 |
缓冲区 | 工作区的文件修改并执行$ git add XXX 后,修改进入缓冲区 |
分支(master) | 执行$ git commit 后,修改进入到master |
而$ git diff a.txt
比较的是工作区的文件和缓冲区的文件
所以在工作区与缓冲区有差别的情况下,该命令才会显示内容,否则没有任何提示:
$ git diff a.txt
diff --git a/a.txt b/a.txt
index 1ecd5ed..1f79c33 100644
--- a/a.txt
+++ b/a.txt
@@ -2,3 +2,4 @@ hello world
hello world
hello world
hello world
+hello world
$ git diff HEAD -- a.txt
这个命令可查看工作区与master中最新版本的区别