当前位置: 移动技术网 > 科技>操作系统>Linux > Git 版本控制管理(一)

Git 版本控制管理(一)

2019年03月15日  | 移动技术网科技  | 我要评论

    git 是一个分布式版本控制工具,它的作者 linus torvalds 是这样给我们介绍 git  —— the stupid content tracker(傻瓜式的内容跟踪器)

关于 git 的产生背景在此不做讲解,有兴趣的可以搜索一下。

先介绍一下 git 的特点,主要有两大特点:

版本控制:可以解决多人同时开发的代码问题,也可以解决找回历史代码的问题。

分 布 式:git是分布式版本控制系统,同一个git仓库,可以分布到不同的机器上。首先找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。可以自己搭建这台服务器,也可以使用github网站。

1. git 安装

[root@kai ~]# yum install git -y
[root@kai ~]# git --version
git version 1.8.3.1

2.创建一个版本库

新建一个目录git_test,在git_test目录下创建一个版本库,命令如下:

[root@kai ~]# mkdir git_test
[root@kai ~]# cd git_test/
[root@kai git_test]# ll -a
total 0
drwxr-xr-x  2 root root   6 mar 13 21:43 .
dr-xr-x---. 5 root root 228 mar 13 21:43 ..
[root@kai git_test]# git init
initialized empty git repository in /root/git_test/.git/
[root@kai git_test]# ll -a
total 0
drwxr-xr-x  3 root root  18 mar 13 21:44 .
dr-xr-x---. 5 root root 228 mar 13 21:43 ..
drwxr-xr-x  7 root root 119 mar 13 21:44 .git

可以看到在git_test目录下创建了一个.git隐藏目录,这就是版本库目录。

3.版本创建与回退

3.1 基本使用

在git_test目录下创建一个文件code.txt,编辑内容如下:

[root@kai git_test]# vim code.txt
[root@kai git_test]# cat code.txt 
this is the first line

使用如下两条命令可以创建一个版本:

[root@kai git_test]# git commit -m 'version1'

*** please tell me who you are.

run

  git config --global user.email "you@example.com"
  git config --global user.name "your name"

to set your account's default identity.
omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@kai.(none)')
# 出现该错误原因是我们没有配置git库的用户名与邮箱,按提示创建即可。

[root@kai git_test]# git config --global user.email "kai@qq.com"
[root@kai git_test]# git config --global user.name "kai"

# 再次提交
[root@kai git_test]# git commit -m 'version1'
[master (root-commit) 020bf02] version1
 1 file changed, 1 insertion(+)
 create mode 100644 code.txt

使用如下命令可以查看版本记录:

[root@kai git_test]# git log
commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1

继续编辑code.txt,在里面再增加一行:

[root@kai git_test]# vim code.txt
[root@kai git_test]# cat code.txt 
this is the first line
this is the second line

使用如下命令再创建一个版本并查看版本记录:

[root@kai git_test]# git add code.txt 
[root@kai git_test]# git commit -m 'version2'
[master 6280fa5] version2
 1 file changed, 1 insertion(+)
[root@kai git_test]# git log
commit 6280fa584403809ac2078a81120acf33e6bec836
author: kai <kai@qq.com>
date:   thu mar 14 00:58:35 2019 -0400

    version2

commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1
[root@kai git_test]#

现在若想回到某一个版本,可以使用命令:git reset --hard head^。head 表示当前版本,head^ 表示上一个版本,head^^ 表示上上个版本。也可以使用另一种表示方式:head~n 表示前n个版本。

现在若觉得想回到版本1,可以使用如下命令:

[root@kai git_test]# git reset --hard head^
head is now at 020bf02 version1
[root@kai git_test]# git log
commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1
[root@kai git_test]# cat code.txt 
this is the first line
[root@kai git_test]# 

执行命令后使用git log查看版本记录,发现现在只能看到版本1的记录,cat code.txt查看文件内容,现在只有一行,也就是第一个版本中code.txt的内容。

假如我们现在又想回到版本2,可以使用如下命令:git reset --hard 版本号
从上面记录可以看到版本2的版本号为:6280fa584403809ac2078a81120acf33e6bec836
在终端执行如下命令:(输入版本号前几位即可)

[root@kai git_test]# git reset --hard 6280fa5844
head is now at 6280fa5 version2
[root@kai git_test]# git log
commit 6280fa584403809ac2078a81120acf33e6bec836
author: kai <kai@qq.com>
date:   thu mar 14 00:58:35 2019 -0400

    version2

commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1
[root@kai git_test]# cat code.txt 
this is the first line
this is the second line
[root@kai git_test]#

现在发现版本2有回来了,cat code.txt查看其里面的内容和原来的相同。

假如说上面的终端已经关了,也就是我们已经不知道版本2的版本号,该怎么回退版本2?
首先我们退回版本1:

[root@kai git_test]# git reset --hard head^
head is now at 020bf02 version1
[root@kai git_test]# cat code.txt 
this is the first line
[root@kai git_test]#

那么在不知道版本号情况下怎么再回到版本2呢?其实git reflog命令可以查看我们的操作记录。
查到版本2的版本号,我们再使用如下命令进行版本回退,版本重新回到了版本2。

[root@kai git_test]# git reflog
020bf02 head@{0}: reset: moving to head^
6280fa5 head@{1}: reset: moving to 6280fa5844
020bf02 head@{2}: reset: moving to head^
6280fa5 head@{3}: commit: version2
020bf02 head@{4}: commit (initial): version1
[root@kai git_test]# git reset --hard 6280fa5844
head is now at 6280fa5 version2
[root@kai git_test]# git log
commit 6280fa584403809ac2078a81120acf33e6bec836
author: kai <kai@qq.com>
date:   thu mar 14 00:58:35 2019 -0400

    version2

commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1
[root@kai git_test]# 

3.2 工作区和暂存区 

1)工作区(working directory)
电脑中的目录,比如我们的git_test,就是一个工作区。

2)版本库(repository)
工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。

      git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫head。因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在 git commit 就是往master分支上提交更改。
可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

下面上图理解:

前面说了我们把文件往git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

下面在git_test目录下再创建一个文件code2.txt,然后编辑内容,同时修改code.txt的内容:

[root@kai git_test]# vim code2.txt
[root@kai git_test]# cat code2.txt 
the code2 first line
[root@kai git_test]# vim code.txt 
[root@kai git_test]# cat code.txt 
this is the first line
this is the second line
this is the third line

使用如下命令查看当前工作树的状态:

[root@kai git_test]# 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:   code.txt
#
# untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	code2.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@kai git_test]# 

上面提示我们code.txt被修改,而code2.txt没有被跟踪。

我们使用如下命令把code.txt和code2.txt加入到暂存区,然后再执行git status命令,结果如下:

[root@kai git_test]# git add code.txt 
[root@kai git_test]# git add code2.txt 
[root@kai git_test]# git status
# on branch master
# changes to be committed:
#   (use "git reset head <file>..." to unstage)
#
#	modified:   code.txt
#	new file:   code2.txt
#
[root@kai git_test]# 

所以git add命令是把所有提交的修改存放到暂存区。

然后,执行git commit就可以一次性把暂存区的所有修改提交到分支创建一个版本。

[root@kai git_test]# git commit -m 'version3'
[master f18f0cc] version3
 2 files changed, 2 insertions(+)
 create mode 100644 code2.txt
[root@kai git_test]# git log
commit f18f0ccadc62b83fa4c6e2222956ba2f2a0e5230
author: kai <kai@qq.com>
date:   thu mar 14 05:16:31 2019 -0400

    version3

commit 6280fa584403809ac2078a81120acf33e6bec836
author: kai <kai@qq.com>
date:   thu mar 14 00:58:35 2019 -0400

    version2

commit 020bf021ec6d1b77836db4e96541d3659251714e
author: kai <kai@qq.com>
date:   wed mar 13 21:57:42 2019 -0400

    version1
[root@kai git_test]# 

一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的。执行如下命令可以发现:

[root@kai git_test]# git status
# on branch master
nothing to commit, working directory clean
[root@kai git_test]# 

现在我们的版本库变成了这样:

3.3 管理修改

git管理的文件的修改,它只会提交暂存区的修改来创建版本。

编辑code.txt,并使用git add 命令将其添加到暂存区中。

[root@kai git_test]# vim code.txt                                                                                                                                                                                                                           
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
[root@kai git_test]# git add code.txt                                                                                                                                                                                           


继续编辑code.txt,并在其中添加一行。

[root@kai git_test]# vim code.txt
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
this is the new line
[root@kai git_test]#

git commit 创建一个版本,并使用git status查看,发现第二次修改code.txt内容之后,并没有将其添加的工作区,所以创建版本的时候并没有被提交。

[root@kai git_test]# vim code.txt
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
this is the new line
[root@kai git_test]# git commit -m 'version4'
[master 66a9c99] version4
 1 file changed, 1 insertion(+)
[root@kai git_test]# 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:   code.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@kai git_test]# 

3.4 撤销修改

继续上面的操作,提示我们可以使用 git checkout -- <文件> 来丢弃工作区的改动。执行如下命令,发现工作区干净了,第二次的改动内容也没了。

[root@kai git_test]# git checkout -- code.txt 
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
[root@kai git_test]# git status
# on branch master
nothing to commit, working directory clean
[root@kai git_test]# 

我们继续编辑code.txt,并在其中添加如下内容,并将其添加的暂存区。

[root@kai git_test]# vim code.txt
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
the new line
[root@kai git_test]# git add code.txt
[root@kai git_test]# git status
# on branch master
# changes to be committed:
#   (use "git reset head <file>..." to unstage)
#
#	modified:   code.txt
#
[root@kai git_test]# 

git同样告诉我们,用命令 git reset head file 可以把暂存区的修改撤销掉,重新放回工作区。

[root@kai git_test]# git reset head code.txt 
unstaged changes after reset:
m	code.txt
[root@kai git_test]# 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:   code.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@kai git_test]#

现在若想丢弃code.txt的修改,执行如下命令即可。

[root@kai git_test]# cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
the new line
[root@kai git_test]# git checkout -- code.txt 
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
[root@kai git_test]# git status
# on branch master
nothing to commit, working directory clean
[root@kai git_test]#

现在,如果你不但改错了东西,还从暂存区提交到了版本库,则需要进行版本回退。

回退小结:
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset head file,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节。

3.5 对比文件不同

对比工作区和某个版本中文件的不同

继续编辑文件code.txt,在最后添加一行 the new line,然后对比工作区中code.txt和head版本中code.txt的不同。使用如下命令:

[root@kai git_test]# echo "the new line" >> code.txt 
[root@kai git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
the new line
[root@kai git_test]# git diff head -- code.txt 
diff --git a/code.txt b/code.txt
index 66f9219..324317f 100644
--- a/code.txt  # - 代表head版本中的 code.txt 内容
+++ b/code.txt  # - 代表工作区中的 code.txt 内容
@@ -2,3 +2,4 @@ this is the first line this is the second line this is the third line this is the forth line +the new line # 工作区的比head版本中的多了一行 [root@kai git_test]#

丢弃工作区的修改

[root@kai git_test]# git checkout -- code.txt 
[root@kai git_test]# git status
# on branch master
nothing to commit, working directory clean

对比两个版本间文件的不同:

[root@kai git_test]# git diff head head^ -- code.txt 
diff --git a/code.txt b/code.txt
index 66f9219..01e1274 100644
--- a/code.txt  # - 代表 head 版本中的 code.txt 内容
+++ b/code.txt  # - 代表 head^ 版本中的 code.txt 内容
@@ -1,4 +1,3 @@
 this is the first line
 this is the second line
 this is the third line
-this is the forth line  # head 版本的比 head^ 版本中的多了一行
[root@kai git_test]#

3.6 删除文件

我们把目录中的code2.txt删除。

[root@kai git_test]# rm -f code2.txt

这个时候,git知道删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻提示哪些文件被删除了。

[root@kai git_test]# git status
# on branch master
# changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    code2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@kai git_test]#

现在有两个选择,一是确实要从版本库中删除该文件,那就用命令 git rm 删掉,并且 git commit:

[root@kai git_test]# git rm code2.txt
rm 'code2.txt'
[root@kai git_test]# git status
# on branch master
# changes to be committed:
#   (use "git reset head <file>..." to unstage)
#
#    deleted:    code2.txt
#
[root@kai git_test]# git commit -m 'delete_code2.txt'
[master f25e944] delete_code2.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 code2.txt
[root@kai git_test]# git status
# on branch master
nothing to commit, working directory clean
[root@kai git_test]#

另一种情况是删错了,可以直接使用git checkout – code2.txt,这样文件code2.txt又回来了

删除小结:

命令 git rm 用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网