linux怎么查看本机内存大小
201
2022-11-10
git应用快速入门
1. git简介
1.1 产生历史
git是目前世界上最先进的分布式版本控制系统。
Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了。Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与的,这么多人在世界各地为Linux编写代码,那Linux的代码是如何管理的呢?事实是,在2002年以前,世界各地的志愿者把源代码文件通过diff的方式发给Linus,然后由Linus本人通过手工方式合并代码!
你也许会想,为什么Linus不把Linux代码放到版本控制系统里呢?不是有CVS、SVN这些免费的版本控制系统吗?因为Linus坚定地反对CVS和SVN,这些集中式的版本控制系统不但速度慢,而且必须联网才能使用。有一些商用的版本控制系统,虽然比CVS、SVN好用,但那是付费的,和Linux的开源精神不符。不过,到了2002年,Linux系统已经发展了十年了,代码库之大让Linus很难继续通过手工方式管理了,社区的弟兄们也对这种方式表达了强烈不满,于是Linus选择了一个商业的版本控制系统BitKeeper,BitKeeper的东家BitMover公司出于人道主义精神,授权Linux社区免费使用这个版本控制系统。安定团结的大好局面在2005年就被打破了,原因是Linux社区牛人聚集,不免沾染了一些梁山好汉的江湖习气。开发Samba的Andrew试图破解BitKeeper的协议(这么干的其实也不只他一个),被BitMover公司发现了(监控工作做得不错!),于是BitMover公司怒了,要收回Linux社区的免费使用权。Linus可以向BitMover公司道个歉,保证以后严格管教弟兄们,嗯,这是不可能的。
实际情况是这样的:Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!一个月之内,Linux系统的源码已经由Git管理了!牛是怎么定义的呢?大家可以体会一下。Git迅速成为最流行的分布式版本控制系统,尤其是2008年,GitHub网站上线了,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等等。历史就是这么偶然,如果不是当年BitMover公司威胁Linux社区,可能现在我们就没有免费而超级好用的Git了。
1.2 git的两大特点
版本控制:可以解决多人同时开发的代码问题,也可以解决找回历史代码的问题。 分布式:Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。首先找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。可以自己搭建这台服务器,也可以使用GitHub网站。
2. 安装与配置
2.1 方式1:yum安装命令如下
(此种安装的1.8,版本太老,可以学习用,生成环境建议用第二种方式):

2.2 方式2:编译安装(生产环境建议此种方式)

(3)安装成功后,运行如下命令:

3. 创建一个版本库
新建目录git_test,在git_test目录下创建一个版本库,命令:git init

可以看到在git_test目录下创建了一个.git隐藏目录,这就是版本库目录。
4. 版本创建与回退
4.1 git的工作原理
① 在版本库中创建文件的时候,就有了文件的第一个版本,于此同时会生成一个指针指向这个版本

② 当修改了code.txt中的内容以后,就生成了第二个版本,但是这个版本是依赖上一个版本的,在这个版本中仅仅会记录文件发生了那些修改。在有了一个新版本之后,这个指针会指向最新的版本。

③ 通过修改指针的位置可以实现回退到某个版本,表示方式有两种,一种是用^,一种使用~,如下
l HEAD^ 或者 HEAD~1:表示上一个版本
l HEAD^^ 或者 HEAD~2:表示上两个版本
通过上面的方式回退到第一个版本之后,指针就指向了版本1

4.2 使用
(1) 在git_test目录下创建一个文件code.txt,写入一行内容如下:

(2) 使用如下两条命令可以创建一个版本,创建版本需要两步:
① git add code.txt
② git commit –m '版本1'

(3) 查看版本记录,命令是git log:

(4) 继续编辑code.txt,在里面增加一行。

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


(6) 现在若想回到某一个版本,可以使用如下命令:
git reset --hard HEAD^
其中HEAD表示当前最新版本,HEAD^表示当前版本的前一个版本,HEAD^^表示当前版本的前前个版本,也可以使用HEAD~1表示当前版本的前一个版本,HEAD~100表示当前版本的前100版本。
现在若觉得想回到版本1,可以使用如下命令:



执行命令后使用git log查看版本记录,发现现在只能看到版本1的记录,cat code.txt查看文件内容,现在只有一行,也就是第一个版本中code.txt的内容。
(7) 假如我们现在又想回到版本2,这个时候怎么办?
可以使用如下命令:
git reset --hard 版本号
从上面可以看到版本2的版本号为:

(8) 根据查询到的序号进行版本选择:


现在发现版本2有回来了。可以cat code.txt查看其里面的内容如下:
(9) 假如说上面的终端已经关了改怎么回退版本。
我们在执行如下命令将版本回退到版本1。

下面把终端关了,然后再打开终端,发现之前版本2的版本号看不到了。

那么怎么再回到版本2呢?git reflog命令可以查看我们的操作记录。

可以看到版本2的版本号,我们再使用如下命令进行版本回退,版本重新回到了版本2。

4.3 工作区和暂存区
4.2.1 工作区(Working Directory)
电脑中的目录,比如我们的git_test,就是一个工作区。
4.2.2 版本库(Repository)
l 工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。
l git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
l 因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
l 你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

前面讲了我们把文件往git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
(1) 下面在git_test目录下再创建一文件code2.txt,然后编辑内容如下:


(2) 然后再次编辑code.txt内容,在其中加入一行,编辑后内容如下:

(3) 使用如下命令查看当前工作树的状态:
git status

上面提示我们code.txt被修改,而code2.txt没有被跟踪。
(4) 我们使用如下命令把code.txt和code2.txt加入到暂存区,然后再执行git status命令,结果如下:

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


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

4.3 管理修改
git管理的文件的修改,它只会提交暂存区的修改来创建版本。
(1) 编辑code.txt,并使用git add 命令将其添加到暂存区中。


添加到缓存区

再添加一行

(2) git commit创建一个版本,

此时并使用git status查看,发现状态不是clean

这是因为第二次修改code.txt内容之后,并没有将其添加的工作区,所以创建版本的时候并没有被提交。
4.4 撤销修改
(1) 继续上面的操作,提示我们可以使用格式来撤销操作,也就是丢弃工作区中你的修改:
git checkout -- <文件>
执行如下命令,

发现工作区干净了,

第二次的改动内容也没了。

(1) 前面所演示的是尚未添加到暂存区中的内容进行回滚,其实,即使加入到了暂存区,也是开始可以回滚的。
我们继续编辑code.txt,并在其中添加如下内容 

并将其添加的暂存区

(3)git同样告诉我们,用命令git reset [选项] HEAD file可以把暂存区的修改撤销掉,重新放回工作区。
选项有三个
l --hard:缓存区和工作目录中的内容都回滚到指定的那个版本状态
l --mixed:默认选项,缓存区和你指定的提交同步,但是工作目录不受影响
l --soft:缓存区和工作目录中的内容都不变(仅仅库中的内容回滚的指定版本)
l 使用这些选项的时候,就不能再指定具体的文件了

状态

到这里,仅仅是保存在将暂存区中的内容删除了,接下来就可以将这个文件回滚到修改之前的状态
(4)现在若想丢弃code.txt的修改,执行如下命令即可。


现在,如果你不但改错了东西,还从暂存区提交到了版本库,则需要进行版本回退。
小结:
l 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
l 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
l 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节。
来演示三个选项的作用
先来演示hard选项
1 在版本库中编辑一个文件1,并add到暂存区

2 编辑另一个文件(不保存到暂存区)

3 查看状态,一个在暂存区 一个是未追踪状态

4 用git reset --hard xxxx 回滚到一个状态

5 再次查看状态(此时发现是clean,什么状态的都没有了)

6 这两个文件中新添加的内容都没了

先来演示soft选项
前面部分略
1 执行 reset,回滚到第一个版本

2 此时暂存区和文件中的内容都还在

3 查看当前所处的版本,已经回到了第一个版本,其他的版本都没了

4.5 对比文件的不同
对比工作区和某个版本中文件的不同:
(1) 继续编辑文件code.txt,在其中添加一行内容。

(2) 现在要对比工作区中code.txt和上个版本中code.txt的不同。使用如下命令:

我们也可以和版本库中的第二个版本做比对

对比两个版本间文件的不同:
(1) 现在要对比HEAD和HEAD^版本中code.txt的不同,使用如下命令:

4.6 删除文件
(1) 我们把目录中的code2.txt删除。

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

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



另一种情况是删错了,可以直接使用git checkout – code2.txt,这样文件code2.txt又回来了。
小结:
命令git rm用于删除一个文件。 如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~