GIT介绍
“Talk is cheap, Show me the code.” 说到git不得不提一下它的作者Linus-Linux神话的缔造者,虽然才过不惑之年,但已经两次改变了世界。咳咳...简单膜拜之后,开始进入我们的正题吧 ~
git是分布式版本控制系统,这种优于集中式系统的架构是它流行原因之一。与SVN的集中式版本管理不同的是,git可以让每个客户端保留一个完整的版本,所以当开发者在进行自己的工作时,不必联网,当自己的工作完成之后可以先提交到本地,等待有网络的情况下再提交到服务器。当然这只是git的一个优点,在分支管理方面,git的表现也是其它系统无法望其项背的。
安装部署GIT
GIT可以在我们常见的系统中安装,但是作为一名忠实的Linux爱好者,我更倾向于直接在Linux上进行安装。下面就简单的了解一下Linux安装方式.
这里使用的是CentOS7的系统作为示例:
1
2
<code>[root@work ~]</code><code># uname -a</code>
<code>Linux work 3.10.0-327.28.2.el7.x86_64 </code><code>#1 SMP Wed Aug 3 11:11:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux</code>
查看系统是否安装git:
<code>[root@work ~]</code><code># git</code>
<code>-</code><code>bash</code><code>: git: </code><code>command</code> <code>not found</code>
没有,那就装上吧:
<code>[root@work ~]</code><code># yum install -y git</code>
由于是多人使用的版本控制系统,所以我们要标示上自己的个人账户:
<code>[root@work ~]</code><code># git config --global user.name "trying" # Your name</code>
<code>[root@work ~]</code><code># git config --global user.email "[email protected]" # Your Mail</code>
创建本地git 仓库:
3
4
<code>[root@work ~]</code><code># mkdir gitrepo </code>
<code>[root@work ~]</code><code># cd gitrepo</code>
<code>[root@work gitrepo]</code><code># git init # 初始化git仓库,会在此目录生成一个.git的目录</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/root/gitrepo/</code><code>.git/</code>
创建了本地的git 仓库会生成一个.git的目录,此目录中记录以后所有对代码的改动信息。值得注意的是,所有的版本控制系统只能追踪文本的改动信息,如代码,网页,txt文件等,对于视频,图片这种二进制的文件是无法追踪其具体的变化的。
提交一个文件到git 仓库
在创建了一个git 仓库之后,我们就可以尝试添加一个文件到我们的git仓库中了。
创建一个文件:
<code>[root@work gitrepo]</code><code># echo "Welcome to gitrepo! " > readme.txt</code>
添加一个文件到仓库:
<code>[root@work gitrepo]</code><code># git add readme.txt # 可以一次添加多个文件,也分多次添加</code>
将文件提交到仓库:
<code>[root@work gitrepo]</code><code># git commit -m "my first git file"</code>
<code>[master (root-commit) 031cca7] my first git </code><code>file</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+)</code>
<code> </code><code>create mode 100644 readme.txt</code>
执行了git commit之后,文件就提交到仓库了,-m 参数是对本此提交的一个说明,在实际的开发过程中,需要对每次提交的内容作说明,这样更加方便识别和管理。
当对文件再次改动:
<code>[root@work gitrepo]</code><code># echo "change it +1" >> readme.txt </code>
<code>[root@work gitrepo]</code><code># cat readme.txt </code>
<code>Welcome to gitrepo! </code>
<code>change it +1</code>
使用git status和git diff 命令可以时刻掌握文件修改的状态和内容:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code>[root@work gitrepo]</code><code># git status</code>
<code># On branch master</code>
<code># Changes not staged for commit: # 显示文件已经被更改,但是还没有提交。</code>
<code># (use "git add <file>..." to update what will be committed)</code>
<code># (use "git checkout -- <file>..." to discard changes in working directory)</code>
<code>#</code>
<code># modified: readme.txt</code>
<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>) </code><code># 提示需要add</code>
<code>[root@work gitrepo]</code><code># git diff # 查看修改前后的内容,类似于linux的diff命令</code>
<code>diff</code> <code>--git a</code><code>/readme</code><code>.txt b</code><code>/readme</code><code>.txt</code>
<code>index 42c334d..238398e 100644</code>
<code>--- a</code><code>/readme</code><code>.txt</code>
<code>+++ b</code><code>/readme</code><code>.txt</code>
<code>@@ -1 +1,2 @@</code>
<code> </code><code>Welcome to gitrepo! </code>
<code>+change it +1 </code><code># 新添加的内容</code>
然后在对修改的文件进行提交,同时查看git status 在这个过程中的变化:
<code>[root@work gitrepo]</code><code># git add readme.txt </code>
<code>[root@work gitrepo]</code><code># git status # 执行add命令后,显示需要commit这个文件</code>
<code># Changes to be committed:</code>
<code># (use "git reset HEAD <file>..." to unstage)</code>
<code>[root@work gitrepo]</code><code># git commit -m "change +1"</code>
<code>[master 58a7726] change +1</code>
<code>[root@work gitrepo]</code><code># git status # 提交完成后,显示工作目录为空,没有需要提交的内容。</code>
<code>nothing to commit, working directory clean</code>
版本控制与文件修改
当我们使用git 提交了多个版本,需要查看之前提交的版本信息,可以使用git log命令查看:
19
20
21
22
23
24
<code>[root@work gitrepo]</code><code># git log </code>
<code>commit c78d925cd2ce9836cd607089b958a48c8959b1d6</code>
<code>Author: trying <[email protected]></code>
<code>Date: Mon Dec 12 15:34:19 2016 +0800</code>
<code> </code><code>change +3 </code><code># 从近到远,依次显示提交的commit信息</code>
<code>commit 8d255bedddd881e4fa70b2c2e6e9b6b14e54f41b</code>
<code>Date: Tue Aug 9 01:02:08 2016 +0800</code>
<code> </code><code>change +2</code>
<code>commit 58a7726b7e0f7a05b6b8077578e4085b423956d1</code>
<code>Date: Tue Aug 9 00:41:55 2016 +0800</code>
<code> </code><code>change +1</code>
<code>commit 031cca7e6021aee8c8a4fba49790c59c53990a4f</code>
<code>Date: Tue Aug 9 00:08:34 2016 +0800</code>
<code> </code><code>my first git </code><code>file</code>
git log命令显示从最近到最远的提交日志,如果想简化输出可以使用 --pretty=oneline参数:
<code>[root@work gitrepo]</code><code># git log --pretty=oneline</code>
<code>c78d925cd2ce9836cd607089b958a48c8959b1d6 change +3</code>
<code>8d255bedddd881e4fa70b2c2e6e9b6b14e54f41b change +2</code>
<code>58a7726b7e0f7a05b6b8077578e4085b423956d1 change +1</code>
<code>031cca7e6021aee8c8a4fba49790c59c53990a4f my first git </code><code>file</code>
查看这些有什么用呢,哈哈,相信聪明的你已经发行了,这里输出的日志中都包含了一个ID,通过这个ID 我们就可以回退到我们想要的版本,如我们要回退到上一个版本,git reset:
<code>[root@work gitrepo]</code><code># git reset --hard 8d255be</code>
<code>HEAD is now at 8d255be change +2</code>
<code>[root@work gitrepo]</code><code># cat readme.txt # 查看文件发现已经变回之前的内容</code>
<code>change +2</code>
这里使用的是指定ID来回退到指点的版本,其实我们还有一个更加便捷的方法,可以使用HEAD^来回退:
<code>[root@work gitrepo]</code><code># git reset --hard HEAD^ # HEAD^前一个版本 HEAD^^前两个版本</code>
<code>HEAD is now at 58a7726 change +1</code>
这里用 “^” 来指定版本,几个“^”就表示回退几次,当然,如果回退的版本有点多可以使用HEAD~100,回退到100次版本前。
查看日志:
<code>[root@work gitrepo]</code><code># git log --pretty=oneline</code>
通过查看日志,我们发现我新编辑的更新没有了,如果ID也没记住,那岂不是回不到最新的内容了/(ㄒoㄒ)/~~ ,别担心git都帮我们想到了,使用git reflog:
<code>[root@work gitrepo]</code><code># git reflog</code>
<code>58a7726 HEAD@{0}: reset: moving to HEAD^</code>
<code>8d255be HEAD@{1}: reset: moving to 8d255be</code>
<code>c78d925 HEAD@{2}: commit: change +3</code>
<code>8d255be HEAD@{3}: commit: change +2</code>
<code>58a7726 HEAD@{4}: commit: change +1</code>
<code>031cca7 HEAD@{5}: commit (initial): my first git </code><code>file</code>
发现了吧,我们所有的操作都被记录了,这里面就包含了每次操作的ID,这样,我们又可以找到我们最新的版本,“快进” 到未来:
<code>[root@work gitrepo]</code><code># git reset --hard c78d925</code>
<code>HEAD is now at c78d925 change +3</code>
<code>change it 3</code>
这样通过git reset --hard 和 git log,git reflog 命令我们可以让文件回到过去的任意状态。
GIT缓存区和工作区
在使用git add 命令的时候,实际上是将我们的编辑的文件提交到GIT的缓存区,然后再使用git commit 命令将文件从缓存区提交到本地仓库,如果不执行git add 添加文件,那么缓存区就不会记录此文件,后面再次更改文件内容时就不会在执行git commit时提交。 如果对文件进行了git add 之后,再次对文件修改,需要重新执行git add 添加此文件,最后执行git commit 的时候,会记录最后一次git add 的文件更改。
撤销工作区修改内容:
有时会出现这样一种情况,在我们的工作区(git仓库目录)修改了大量文件,但是最后你发现文件全部都改错了! 是的,遇到这种尴尬的情况应该怎么办呢,回退到上一版本?显然这样不科学,尤其是本地没有上一版本的时候,其实不必担心,此时执行git status就能获得提示:
<code># Changes not staged for commit:</code>
<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>
提示告诉我们要撤销工作目录改变的文件可以使用git checkout -- <file>:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt #一条命令即可</code>
撤销暂存区修改内容:
比上面更加尴尬的一种情况是,不仅修改了工作区的内容,而且还将错误的内容使用git add 提交到了暂存区,这时候怎么办呢,让我们再看看git status 说什么吧:
提示我们使用git reset HEAD <file>来从暂存区撤销,试一下效果:
<code>[root@work gitrepo]</code><code># git reset HEAD readme.txt </code>
<code>Unstaged changes after reset:</code>
<code>M readme.txt</code>
<code>[root@work gitrepo]</code><code># git status # 查看撤销后的状态,回到了git add之前的情况</code>
然后在再使用上面的命令,撤销工作区的更改:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt </code>
这样,所有的更改都已经撤销了。
如果不幸执行了commit命令,可以通过版本回滚版本来回到之前的状态,但是如果已经推送到远程仓库,那就呵呵了。如果改动直接上线,可以通过持续集成服务器,可以快速恢复,不过此时或多或少会影响到线上的业务。
删除文件
如果我们想删除本地版本库中的文件,可以直接执行下面两条命令:
<code>[root@work gitrepo]</code><code># rm -f aa.txt # 删除本地文件</code>
<code>[root@work gitrepo]</code><code># git rm aa.txt # 删除版本库文件,并且commit提交</code>
<code>rm</code> <code>'aa.txt'</code>
<code>[root@work gitrepo]</code><code># git commit -m "del aa.txt"</code>
<code>[master 59f04b4] del aa.txt</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 0 insertions(+), 0 deletions(-)</code>
<code> </code><code>delete mode 100644 aa.txt</code>
使用git rm <file> 和 git commit 来删除版本库中的文件。
但是如果本地文件不小心删除错了,只要我们版本库中提交过就可以快速恢复:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt</code>
<code>[root@work gitrepo]</code><code># rm -f readme.txt </code>
<code>[root@work gitrepo]</code><code># ll</code>
<code>total 0</code>
<code># (use "git add/rm <file>..." to update what will be committed)</code>
<code># deleted: readme.txt</code>
<code>total 4</code>
<code>-rw-r--r--. 1 root root 69 Dec 12 18:36 readme.txt </code><code># 文件又回来了!</code>
本文转自 酥心糖 51CTO博客,原文链接:http://blog.51cto.com/tryingstuff/1882034