Git 版本控制管理(一)

Stella981
• 阅读 484

    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版本库里添加的时候,是分两步执行的:
第一步是用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]#

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

Git 版本控制管理(一)

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这