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

Git 版本控制管理(一)的更多相关文章

  1. git版本控制管理实践-4

    vcs: version control system 版本控制系统 local vcs, 集中式版本控制系统: centralized vcs; 分布式vcs: distributed vcs Lo ...

  2. Git 版本控制管理(二)

    Git 分支管理 1. 概念 我们来抽象的理解,分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN.如果两个平行宇宙互不干扰,那对现在的你也 ...

  3. 利用Git版本控制管理你的项目

    准备工作 项目创建完成后,将项目版本控制起来,今后每个版本的迭代会非常清楚,同时也有助于项目进行协同开发. 还有一个十分重要的问题是:项目上线后,线上的运行的程序的配置与线下进行测试的配置文件是不一样 ...

  4. Git版本控制管理学习笔记1-介绍

    几乎所有的版本控制工具都是出于同样的目的:开发以及维护开发出来的代码,方便读取代码的历史,记录所有的修改.这里,介绍的是当前在开源社区内非常流行的版本控制工具Git.它是由Linus Torvalds ...

  5. Git版本控制管理教程(一):介绍

    我的个人博客地址:damienzhong.com 原文链接:点击打开链接 1.1 背景 数据是短暂的,且容易丢失的,特别是作为开发人员的我们每天频繁的对项目代码进行更新,容易产生错误代码的变更或者项目 ...

  6. Git版本控制管理学习笔记4-文件管理和索引

        可以认为使用Git时,我们会遇到3个空间:工作目录.索引.版本库.我们关心的,就是在新建.修改等操作时,这三者之间发生了怎样的变化.     笼统的讲,就是在工作目录下编辑,在索引中积累修改, ...

  7. Git版本控制管理学习笔记3-基本的Git概念

    为了更近一步的学习和理解Git的理念,这一节介绍一下Git中的一些基本概念. 基本概念 对象库图示 Git在工作时的概念 一.基本概念: 1.版本库:     Git的版本库就是一个简单的数据库,其中 ...

  8. Git版本控制管理学习笔记2--起步

    首先确保系统中已经安装了git,这里使用的linux系统. 一.命令行初步使用: 1.git命令: 列出它的选项和最常用的子命令.标准命令格式中,COMMAND代表的就是下面列出的子命令. [root ...

  9. Git版本控制管理学习笔记5-提交

        这个标题其实有些让人费解,因为会想这个提交是动词还是名称?     提交动作是通过git commit命令来实现的,提交之后会在对象库中新增一个提交对象.提交过程中会发生哪些变化,在上一篇笔记 ...

随机推荐

  1. CSS框架960Grid从入门到精通一步登天

    http://blog.chinaunix.net/uid-22414998-id-2878529.html 1.什么是CSS框架?     CSS框架是一种你能够使用在你的web项目中概念上的结构, ...

  2. OpenStack 网络服务 Neutron 多网卡(提供者网络)(十八)

    OpenStack 网络服务 Neutron 多网卡,分为内部网络.外部网络 使用vmware模拟两张网卡 添加网卡 网卡配置 cd /etc/sysconfig/network-scripts cp ...

  3. Hadoop生态圈-zookeeper本地搭建以及常用命令介绍

    Hadoop生态圈-zookeeper本地搭建以及常用命令介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.下载zookeeper软件 下载地址:https://www.ap ...

  4. SqlParameter防止SQL注入

    SQL注入的解决方案有好几种,待我细细研究过之后逐一讲解. 方法一:SqlParameter方法 这里有一篇博客是详细介绍SqlParameter的,可以看看 点我 string sqlStr=&qu ...

  5. ubuntu 环境下pycharm的 安装与激活教程 以及错误解决方法

    1. 基本安装: 1.1 打开Ubuntu的应用市场,并在搜索栏搜索pycharm,结果如下图所示 1.2 选择pro版本进行安装,结果如下图所示: 1.3打开安装后的pycharm,如果出现下图所示 ...

  6. [整理]Visual Studio 的Application Insights

    简单介绍 Application Insights(预览版) Visual Studio 的Application Insights插件简介 Application Insights for Visu ...

  7. AngularJS入门基础——表单验证

    <form name="form" novalidata>   <label name="email">your email</l ...

  8. bzoj3029 守卫者的挑战 (多维dp)

    题面: 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过."我,Nizem,是黑魔法圣殿的守卫者.如果你能通过我的挑 ...

  9. Python 入门基础12 --函数基础5 匿名函数、内置函数

    今日内容: 1.三元表达式 2.列表.元组生成式 | 字典生成式 3.递归 4.匿名函数 5.内置函数 一.三元表达式 三元运算符:就是 if...else... 语法糖 前提:if 和 else # ...

  10. [Alg::DP] 袋鼠过河

    一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...