Git 入门
Git简介、安装
分布式版本控制系统
macOS自带,故略
版本库
版本库是一个仓库(respository),记录文件的修改删除,是文件历史 管理器
创建目录
mkdir learngit
cd learngit
pwd
/Users/taocheung/Git/learngit
初始化
git init
Initialized empty Git repository in /Users/taocheung/Git/learngit/.git/
ls -a
告诉我这是一个空的仓库,查看了隐藏目录后发现有一个.git文件,勿动
添加文件
珍爱生命,远离巨硬
前提
Git只跟踪文本文件的改动,对于图片和视频这些二进制文件,只去追踪大小的改变。
Microsoft的word就是二进制格式,所以不要用Git去管理word,这是其一
其二,文本都是有编码的,中文常用GBK,建议使用UTF-8。Microsoft的文本文件,添加了十六进制字符,会出错。
正文
由于macOS的局限性,所以改创建一个markdown文件
在learngit下创建一个文件readme.md
添加内容
Git is a version control system.
Git is free software.
之后提交文件
git add readme.md
无任何显示
用git commit
告诉Git,把文件提交到仓库
git commit -m "wrote a readme file"
1 file changed, 3 insertions(+)
create mode 100644 readme.md
表示一个文件被改动,插入了三行内容(我的光标在第三行)
时光机穿梭
现在修改文件
Git is a distributed version control system.
Git is free software.
使用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: readme.md
no changes added to commit (use "git add" and/or "git commit -a")
在master分支,“changes”还没有被提交
如何查看修改了什么内容?git diff
diff --git a/readme.md b/readme.md
index 8d7a2fb..14bbe9c 100644
--- a/readme.md
+++ b/readme.md
@@ -1,3 +1,4 @@
-Git is a version control system.
+Git is a distributed version control system.
+
+Git is free software.
-Git is free software.
\ No newline at end of file
(END)
按q退出
知道了修改了什么之后再进行提交就放心了
git add readme.md
在提交到仓库之前来看一下状态
git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.md
Changes to be committed,改变即将被commit
git commit -m "add distributed"
git commit --amend --reset-author
1 file changed, 3 insertions(+), 2 deletions(-)
再查看状态
$ git status
On branch master
nothing to commit, working tree clean
working tree clean
工作区与暂存区
Git具有工作区和暂存区的概念
- 工作区就是能看到的目录
工作区有一个隐藏文件.git,是版本库
版本库里有很多东西,其中包括暂存区stage、分支master、指针HEAD

我们把文件往Git版本库里添加的时候,是分两步执行的:
- 用
git add
把文件添加进去,实际上就是把文件修改添加到暂存区; - 用
git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master
分支,所以,现在,git commit
就是往master
分支上提交更改。
练习
增加一行内容
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
增加一个文件
LICENSE.md
查看状态
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: readme.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE.md
Readme.md被修改,LICENSE.md未被tracked
使用命令git add .
,全部添加
再次查看状态
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: LICENSE.md
modified: readme.md
现在的暂存区

使用commit 提交
git commit -m "understand how stage works"
[master eeb0409] understand how stage works
2 files changed, 2 insertions(+)
create mode 100644 LICENSE
此时工作区很干净

暂存区中没有内容了