Git分布式版本控制常用命令解析
一、创建版本库
版本库(repository)也叫仓库,可以看做一个目录,这个目录里的所以文件都由Git进行管理,每个文件的修改、删除,Git都能跟踪
1、选择一个合适的地方,创建一个空目录:
[root@localhost ~]# mkdir villian ##创建一个空目录作为本地库##
[root@localhost ~]# cd villian ##进入到该目录对该目录进行初始化##
如果使用Windows系统,要保证目录名不包含中文
2、通过git init命令把该目录变成Git可以管理的仓库:
[root@localhost villian]# git init ##对本地库进行初始化##
初始化空的 Git 版本库于 /root/villian/.git/ ##中文解析##
Initialized empty Git repository in /root/villian/.git/ ##英文解析##
二、创建文件、并导入版本库
1、在villian目录(工作区)中编写一个home.txt文件:
vi home.txt
My name is villian
I am from shenzhen
2、使用 git status 命令查看当前状态
[root@localhost villian]# git status
位于分支 master
初始提交
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
home.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
通过git status命令可知,home.txt命令正处于工作区中
3、使用 git add 命令,把文件提交到(暂存区)中:
[root@localhost villian]# git add home.txt
[root@localhost villian]# git status
位于分支 master
初始提交
要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)
新文件: home.txt
通过git status命令可知,home.txt命令正处于仓库(暂存库)中
4、1使用 gir rm --cached 命令,把文件从暂存区恢复到工作区中:
[root@localhost villian]# git rm --cached home.txt
rm 'home.txt'
[root@localhost villian]# git status
位于分支 master
初始提交
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
home.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
4、2使用 git commit 命令,把文件提交到(本地库)中:
[root@localhost villian]# git commit -m "home_first" home.txt
位于分支 master
无文件要提交,干净的工作区
[root@localhost villian]# git status
位于分支 master
无文件要提交,干净的工作区
三、修改文件
我们已经成功添加并提交了一个 home.txt 文件,现继续修改 readme.txt 文件
1、向文本中添加一行“I am from Guangdong”信息
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong ##新增一行信息##
2、使用 git diff 命令查看本次修改变动内容
[root@localhost villian]# git diff home.txt
diff --git a/home.txt b/home.txt
index 8c8045d..f3ab093 100644
--- a/home.txt
+++ b/home.txt
@@ -1,2 +1,3 @@
My name is villian
I am from shenzhen
+I am from Guangdong ##此处新增一行##
3、使用 git status 命令查看当前状态
[root@localhost villian]# git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: home.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
此次状态与新建文件时状态类似,但是多了一行撤销(丢弃)修改的提示
4、1使用 git chekout 命令,将本次修改撤销
[root@localhost villian]# git checkout home.txt
[root@localhost villian]# git status
位于分支 master
无文件要提交,干净的工作区
使用git chekout 命令后,恢复到了修改之前的状态
通过cat命令查看home.txt文本内容(此时 I am from Guangdong已经不见)
[root@localhost villian]# cat home.txt
My name is villian
I an from shenzhen
4、2使用git add命令将修改后内容提交到暂存库中
[root@localhost villian]# git status ##接上述第2步修改内容##
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: home.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost villian]# git add home.txt ##重新提交至暂存区##
[root@localhost villian]# git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: home.txt
5、使用git commit -m 命令将home.txt从暂存区提交到本地库中
[root@localhost villian]# git commit -m "home_next" home.txt
位于分支 master
无文件要提交,干净的工作区
[root@localhost villian]# git status
位于分支 master
无文件要提交,干净的工作区
四、版本回退
为增加试验效果,此时我已经对home.txt文本修改了5次
1、1使用 git log 查看修改记录
注:一大串数字是 commit id
[root@localhost villian]# git log
commit 7d35d6840844b0e6c96d7d7209ff9f45a1029315
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:50:30 2019 +0800
home_fifth
commit 7a690b414f87fbbe746338d247260021a51a43e2
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:49:16 2019 +0800
home_fourth
commit f9828f5273b60638b9f67f135cd4d4b74f3ddb9f
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:46:55 2019 +0800
home_third
commit 2afde91ff027301499b77cd940b651dda18d5bae
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:38:07 2019 +0800
home_next
commit 9e6906339361841d1c8b27a4cca91ddc4dba0b99
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:04:23 2019 +0800
home_first
1、2 使用–pretty=oneline 参数,单行显示修改记录
[root@localhost villian]# git log --pretty=oneline
7d35d6840844b0e6c96d7d7209ff9f45a1029315 home_fifth
7a690b414f87fbbe746338d247260021a51a43e2 home_fourth
f9828f5273b60638b9f67f135cd4d4b74f3ddb9f home_third
2afde91ff027301499b77cd940b651dda18d5bae home_next
9e6906339361841d1c8b27a4cca91ddc4dba0b99 home_first
1、3使用 --oneline 参数,更为简洁显示修改记录
[root@localhost villian]# git log --oneline
7d35d68 home_fifth
7a690b4 home_fourth
f9828f5 home_third
2afde91 home_next
9e69063 home_first
2、使用 git reset --hard 命令修改版本
[root@localhost villian]# git reflog
7d35d68 HEAD@{0}: commit: home_fifth
7a690b4 HEAD@{1}: commit: home_fourth
f9828f5 HEAD@{2}: commit: home_third
2afde91 HEAD@{3}: commit: home_next
9e69063 HEAD@{4}: commit (initial): home_first
[root@localhost villian]# git reset --hard 7a690b4 ##恢复到第四版本##
HEAD 现在位于 7a690b4 home_fourth
## 使用cat home.txt查看文本内容是否已变化 ##
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
最后一个 “I am from the earth”已经不见
五、工作区和暂存区
-
工作区(Working Directory)
learngit 文件夹就是一个工作区。 -
版本库(Repository)
工作区有个隐藏目录 .git ,这个不算工作区,而是 Git 的版本库。 -
版本库里面的 index(stage) 文件叫暂存区,还有Git为我们自动创建的第一个分支 master ,以及指向 master 的一个指针叫做 HEAD。
前面我们提到过,如果我们想把文件添加到Git里面时,需要分两步:
-
第一步是用 git add 把文件添加进去,实际上就是把文件修改添加到暂存区。
-
第二步是用 git commit 提交更改,实际上就是把暂存区的所有内容提交到当前分支。(我们现在只有唯一一个分支 master,所以现在就是往 master 分支上提交更改)
我们可以实践一下:
- 1、在 readme.txt 文件中加上一行内容:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
- 2、然后在工作区新建一个 LICENSE 文本文档(任意内容)
使用两次 git add 命令分别把 readme.txt 和 LICENSE 都添加后,可以用 git status 命令查看一下:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: readme.txt
- 3、现在,暂存区的状态就变成这样了:
- 4、再使用 git commit 命令把暂存区的所有修改提交到分支:
$ git commit -m "understand how stage works"
[master e43a48b] understand how stage works
2 files changed, 2 insertions(+)
create mode 100644 LICENSE
- 5、这时候的工作区就是干净的:
$ git status
On branch master
nothing to commit, working tree clean
六、撤销修改
假如说你在 home.txt 文件中添加了一行内容如下:
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
Shenzhen is a very beautiful city
想要删除,应该怎么撤销呢?
1、没有 git add 之前
- 通过手动进入Vim编辑器删除最后一行,手动把文件恢复到上一个版本的状态。
- 然后再使用 git checkout file 命令丢弃工作区的修改:
$ git checkout home.txt //把home.txt文件在工作区的修改全部撤销。
- 现在看一下 readme.txt 文件内容:
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
[root@localhost villian]# git status
位于分支 master
无文件要提交,干净的工作区
2、使用 git add 提交到暂存区,但没有使用 git commit 提交到版本库中
- 这时候的修改添加到了暂存区,但没有提交到分支,用 git status 查看一下:
[root@localhost villian]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
- 这时候我们可以使用 git reset HEAD file 命令把把暂存区的修改撤销掉,重新放回工作区:
[root@localhost villian]# git reset HEAD home.txt //git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区,HEAD表示最新版本。
Unstaged changes after reset:
M readme.txt
- 现在再用 git status 查看一下:
[root@localhost villian]# git status
On branch master
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: readme.txt //暂存区是干净的,工作区有修改
- 这时候再丢弃工作区的修改就OK了:
[root@localhost villian]# git checkout home.txt //丢弃工作区的修改
[root@localhost villian]# git status
On branch master
nothing to commit, working tree clean
3、 既使用 git add 提交到了暂存区,也使用 git commit 提交到了版本库
- 使用 git reset 命令回退版本
七、删除文件
- 在工作区即 villian 目录下新建一个 test.txt 文件,并添加和提交到Git:
[root@localhost villian]# git add test.txt
[root@localhost villian]# git commit -m "add test.txt"
[master b84166e] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
- 这时候可用 rm 命令删除:
[root@localhost villian]# rm test.txt
这时工作区和版本库就不一样了
现在又分两种情况:
1、 确实要从版本库中删除该文件,那就用 git rm 命令删除,并且 git commit:
[root@localhost villian]# git rm test.txt
rm 'test.txt'
[root@localhost villian]# git commit -m "remove test.txt"
[root@localhost villian]# remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
- 这时候文件就从版本库被删除了。
2、文件被删错了。因为版本库里有,所以很好恢复:
[root@localhost villian]# git checkout test.txt //用版本库里的版本替换工作区的版本。
八、比较文件
比较谁跟谁的区别,就将该文件提交至该区域,然后再修改文件提交至比较区域
例如:暂存区与版本库的区别,就先将文本提交至版本库中,然后再修改该文件,提交至暂存区,最后通过相应的命令(git diff --cached id)进行对比;
- 创建zwl.txt文本,内容如下:
[root@localhost villian]# cat zwl.txt
my name is zwl
- 然后将该文本提交至暂存区中
[root@localhost villian]# git add zwl.txt
1、比较工作区与暂存区的区别
- 修改工作区中zwl.txt文本内容,向文本中新增“i am from shenhen”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
- 通过 git diff 命令进行对比
[root@localhost villian]# git diff zwl.txt
diff --git a/zwl.txt b/zwl.txt
index fbbccbf..8cb4939 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1 +1,2 @@
my name is zwl ///此处为新增内容
+i am from shenzhen
2、比较工作区与版本库的区别
- 将zwl.txt从暂存区提交至版本库中
[root@localhost villian]# git commit -m "zwl" zwl.txt
- 修改工作区zwl.txt文本内容,新增“hello”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
hello
- 通过git log --oneline 命令查看commit_id号
[root@localhost villian]# git log --oneline
93b356b zwl1
484c46a zwl
通过下述命令对比工作区和版本库中的区别
- git diff 484c
- git diff HEAD // 如果是与最新的提交比较,commit id可以使用 HEAD代替
[root@localhost villian]# git diff 484c
diff --git a/zwl.txt b/zwl.txt
index 8cb4939..d624612 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1,2 +1,4 @@
my name is zwl
i am from shenzhen
+i am 18
+hello
3、比较暂存区与版本库文件的区别
- 修改版本库中的zwl.txt文本内容,新增“2019”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
i am 18
hello
2019
- 将zwl.txt从工作区提交至暂存区
[root@localhost villian]# git add zwl.txt
通过下述命令对比暂存区和版本库中的区别
- git diff --cached 484c
- git diff–cached HEAD // 如果是与最新的提交比较,commit id可以使用 HEAD代替
[root@localhost villian]# git diff --cached HEAD
diff --git a/zwl.txt b/zwl.txt
index 71f8256..aef3448 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1,3 +1,5 @@
my name is zwl
i am from shenzhen
i am 18
+hello
+2019