Git入门
简介
Git是一个免费开源的分布式版本控制系统,用于管理项目的方方面面,能够高速、高效地用于从小项目到非常大的项目。
工作流程及术语
- 工作空间(workspace):当前项目文件夹
- 索引(index):工作空间索引
- 本地仓库(localrepo):本地保存历史版本内容的数据库
- 远程仓库(remoterepo):远程保存历史版本内容的数据库
本地工作流演示
- 设置个人身份
git config --help
git config --global user.name "<user name>"
git config --global user.email <email>
- 初始化本地仓库
[titi@mine test]$ git init
Initialized empty Git repository in /home/titi/test/.git/
- 新建文件
[titi@mine test]$ echo 1 > text
[titi@mine test]$ git status -s
?? text
- 添加至索引
[titi@mine test]$ git add text
[titi@mine test]$ git status -s
A text
- 添加至本地仓库
[titi@mine test]$ git commit -m "The first commit"
[master (root-commit) 9513892] The first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 text
[titi@mine test]$ git log
commit 9513892bd948ebaa7c12cb0c6c5d4a7814a77433
Author: Dazhi Jiang <ejdazhi@163.com>
Date: Sun Oct 30 15:23:32 2016 +0800
The first commit
- 直接添加至本地仓库
[titi@mine test]$ echo 111 > text
[titi@mine test]$ git status -s
M text
[titi@mine test]$ git commit -am 'the second commit'
- 更改文件并添加至索引
[titi@mine test]$ echo 222 >> text
[titi@mine test]$ cat text
111
222
[titi@mine test]$ git add text
- 再次更改文件
[titi@mine test]$ echo 333 >> text
[titi@mine test]$ cat text
111
222
333
- 本地仓库与工作空间差异
[titi@mine test]$ git diff HEAD
diff --git a/text b/text
index 58c9bdf..641d574 100644
--- a/text
+++ b/text
@@ -1 +1,3 @@
111
+222
+333
- 索引与工作空间差异
[titi@mine test]$ git diff
diff --git a/text b/text
index a30a52a..641d574 100644
--- a/text
+++ b/text
@@ -1,2 +1,3 @@
111
222
+333
- 重置工作空间至索引
[titi@mine test]$ git checkout text
[titi@mine test]$ cat text
111
222
- 重置工作空间至本地仓库最后版本
[titi@mine test]$ git checkout HEAD text
[titi@mine test]$ cat text
111