一、创建版本库
1.创建空目录
MrZhou@LAPTOP-IVKUMD15 MINGW64 ~
$ cd /e/
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e
$ mkdir gitLibrary
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e
$ cd gitLibrary
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary
$ pwd
/e/gitLibrary
命令 | Function |
---|---|
mkdir | 创建文件夹 |
cd | 切换目录 |
pwd | 显示当前目录 |
2.初始化Git仓库
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary
$ git init
Initialized empty Git repository in E:/gitLibrary/.git/
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ ls -ah
./ ../ .git/
命令 | Function |
---|---|
git init | 把当前目录变成Git可以管理的仓库 |
ls -ah | 显示隐藏目录 |
3.把文件添加到版本库
第一步:git add 命令, 添加文件到仓库
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git add readme.txt
第二步:git commit 命令,提交文件到仓库
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git commit -m "wrote a readme file"
[master (root-commit) 7c5d210] wrote a readme file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
命令 | Function |
---|---|
git add | 添加文件到仓库 |
git commit -m " " | 提交文件到仓库,-m后面输入的是本次提交的说明 |
二、版本穿梭
1.回到过去
修改文件
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ vi readme.txt
命令 | Function |
---|---|
vi | 编辑文档 |
按Esc 输入:wq | 保存退出 |
原文本内容为:
Git is a version control system.
Git is free software.
修改文本内容为:
Git is a distributed version control system.
Git is free software.
查看上次修改内容
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index e69de29..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -0,0 +1,2 @@
+Git is a distributed version control system.
+Git is free software.
命令 | Function |
---|---|
git diff | 查看上次修改 |
查看创库当前的状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ 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.txt
no changes added to commit (use "git add" and/or "git commit -a")
上面的命令输出:readme.txt被修改过,但没有提交修改。
命令 | Function |
---|---|
git status | 仓库当前的状态 |
提交修改后,查看当前仓库的状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git add readme.txt
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
上面的命令输出:可以提交新文件
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git commit -m"add distributed"
[master 9791f15] add distributed
1 file changed, 2 insertions(+)
提交后再看看仓库的当前状态
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git status
On branch master
nothing to commit, working tree clean
上面命令输出:当前没有需要提交的修改,而且,工作目录是干净的
再次修改文本内容:
Git is a distributed version control system.
Git is free software distributed under the GPL.
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ vi readme.txt
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ 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.txt
no changes added to commit (use "git add" and/or "git commit -a")
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git add readme.txt
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git commit -m"append GPL"
[master ead6c99] append GPL
1 file changed, 1 insertion(+), 1 deletion(-)
不断对文件进行修改,然后不断提交修改到版本库里,就好比玩游戏时,每通过一关就会自动把游戏状态存盘,如果某一关没过去,你还可以选择读取前一关的状态。有些时候,在打Boss之前,你会手动存盘,以便万一打Boss失败了,可以从最近的地方重新开始。
Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失。
现在有三个版本:
版本1:wrote a readme file
版本2:add distributed
版本3: append GPL
在实际工作中,我们不可能记得每次改动了什么内容,所以版本控制系统里的git log可以告诉我们历史记录。
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git log
commit ead6c997dc9dd6455f3ebf9903ebf85226e7ce13 (HEAD -> master)
Author: Zhoujy <17372833873@163.com>
Date: Fri Oct 9 18:57:12 2020 +0800
append GPL
commit 9791f15c72dd3be129899686a536e5e8f954213d
Author: Zhoujy <17372833873@163.com>
Date: Fri Oct 9 18:43:05 2020 +0800
add distributed
commit 7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa
Author: Zhoujy <17372833873@163.com>
Date: Mon Oct 5 21:50:31 2020 +0800
wrote a readme file
加上- -pretty=oneline参数
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git log --pretty=oneline
ead6c997dc9dd6455f3ebf9903ebf85226e7ce13 (HEAD -> master) append GPL
9791f15c72dd3be129899686a536e5e8f954213d add distributed
7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa wrote a readme file
命令 | Function |
---|---|
git log | 显示提交日志(从最近到最远) |
git log --pretty=oneline | 版本号(commit id 、记录) |
Git版本回退
命令 | Function |
---|---|
HEAD | 表示当前版本 |
HEAD^ | 上个版本 |
HEAD^^ | 上上个版本 |
HEAD~N | 上N个版本 |
回退到上个版本:add distributed
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git reset --hard HEAD^
HEAD is now at 9791f15 add distributed
命令 | Function |
---|---|
git reset | 回退到之前的版本 |
查看文本内容:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ cat readme.txt
Git is a distributed version control system.
Git is free software.
命令 | Function |
---|---|
cat | 显示文本内容 |
此时,查看下版本库的当前状态:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git log
commit 9791f15c72dd3be129899686a536e5e8f954213d (HEAD -> master)
Author: Zhoujy <17372833873@163.com>
Date: Fri Oct 9 18:43:05 2020 +0800
add distributed
commit 7c5d21032f31ab1e3e9f0e3a6017a9d3a63188aa
Author: Zhoujy <17372833873@163.com>
Date: Mon Oct 5 21:50:31 2020 +0800
wrote a readme file
2.返回未来
发现最新的版本3:append GPL看不到了。
当上面的窗口没有关掉时,可以找到版本3的commit id:
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git reset --hard ead6c
HEAD is now at ead6c99 append GPL
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
如果之前的窗口已经关掉,找不到版本3的commit id时,Git提供了一个命令git reflog。
MrZhou@LAPTOP-IVKUMD15 MINGW64 /e/gitLibrary (master)
$ git reflog
ead6c99 (HEAD -> master) HEAD@{0}: reset: moving to ead6c
9791f15 HEAD@{1}: reset: moving to HEAD^
ead6c99 (HEAD -> master) HEAD@{2}: commit: append GPL
9791f15 HEAD@{3}: commit: add distributed
7c5d210 HEAD@{4}: commit (initial): wrote a readme file
命令 | Function |
---|---|
git reflog | 记录了每一次命令 |
总结
- 初始化一个Git仓库,使用git init命令。
- 添加文件到Git仓库,分两步:
1.使用命令git add < file >,注意,可反复多次使用,添加多个文件;
2.使用命令git commit -m < message >,完成。 - 掌握工作区的状态,使用git status命令。
- 如果git status告诉有文件被修改过,用git diff可以查看修改内容
- HEAD指向当前版本,使用git reset --hard commit_id.
- 用git log查看提交历史,以便确定回退到哪个版本
- 重返未来,用git reflog查看命令历史,以便确定回到哪个未来的版本