末学者笔记--Git介绍(一)

Git介绍及常用操作演示(一)

 

一.Git介绍                                                     

 

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。 Git Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

 

Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,所以,工作的时候就不需要联网了,因为版本库都是在自己的电脑 上。现在每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

 

主要有如下特点

 

1. 版本控制

2. 分布式

3. 工作过程是将服务器上的代码下载到本地,本地开发完成后,在提交到服务器端

 

git相比于svn功能更加的强大,命令也很多。

 

二.GitSVN的对比                                                     

 

1.git是分布式的,svn是集中式的。(最核心)

 

2.git是每个历史版本都存储完整的文件,便于恢复,svn是存储差异文件,历史版本不可恢复。(核心)

 

3.git可离线完成大部分操作,svn则不能。

 

4.git有着更优雅的分支和合并实现。

 

5.git有着更强的撤销修改和修改历史版本的能力

 

6.git速度更快,效率更高。

 

基于以上区别,git有了很明显的优势,特别在于它具有的本地仓库。

 

 

三.Git几个概念                                                      

 

1. 工作目录

工作目录是对项目的某个版本独立提取出来的内容。这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。

 

2. 暂存区域

是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。有时候也被称作`‘索引’’,不过一般说法还是叫暂存区域。

 

3. Git 仓库目录

Git 用来保存项目的元数据和对象数据库的地方。这是 Git 中最重要的部分,从其它计算机克隆仓库时,拷贝的就是这里的数据。

 

 

 

四.Git工作流程                                                          

 

基本的 Git 工作流程如下:

 

在工作目录中修改文件 > 暂存文件,将文件的快照放入暂存区域 > 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 仓库目录。

 

如果 Git 目录中保存着的特定版本文件,就属于已提交状态。如果作了修改并已放入暂存区域,就属于已暂存状态。如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态。

 

五.Git的安装                                                        

 

第一种安装方式:

直接yum进行安装

 

——# yum install git -y

 

第二种安装方式:

编译安装

 

第一步:上传安装包并解压

 

——# rz

——# ls | grep git

git-v2.7.4.zip

——# unzip git-v2.7.4.zip

 

第二步:安装依赖

 

[root@root git-2.7.4]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y

 

第三步:进入解压下目录下并执行编译

 

[root@root git-2.7.4]# make prefix=/usr/local/git all

[root@root git-2.7.4]# make prefix=/usr/local/git install

 

第四步:导入二进制程序

 

[root@root git-2.7.4]# rm -rf /usr/bin/git

[root@root git-2.7.4]# ln -s /usr/local/git/bin/git /usr/bin/git

[root@root git-2.7.4]# git --version

git version 2.7.4

 

六.git常用选项介绍                                                      

 

add  添加文件到暂存区域

 

branch 显示分支和创建分支

 

checkout 切换分支以及回滚

 

clone  克隆gitlab仓库

 

commit 提交代码至仓库

 

init 初始化工作目录

 

log 显示历史版本信息

 

merge 合并分支

 

pull 拉取远程代码至本地

 

push 推送本地仓库代码至远程仓库

 

reset 回滚版本

 

status 查看工作目录下的文本状态

 

七.git简单使用演示                                                    

 

第一步:创建一个目录,并进入

 

[root@feigea ~]# mkdir /feifei

[root@feigea ~]# cd /feifei 

 

第二步:初始化目录

 

[root@feigea feifei]# git init

Initialized empty Git repository in /feifei/.git/

[root@feigea feifei]# ls -a

.  ..  .git    #包含该文件的是git的工作目录,所以以后想在git目录下操作,需要到包含.git的目录下进行

 

 

第三步:创建一个测试文件

 

[root@feigea feifei]# echo  "123">>test

 

第四步:进行添加至暂存区域

 

[root@feigea feifei]# git add .   #.表示提交当前目录下的所有文件

 

第五步:提交至仓库

 

[root@feigea feifei]# git log

fatal: bad default revision 'HEAD'

[root@feigea feifei]# git commit -m 'v1'

 

*** 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@feigea.(none)')

 

[root@feigea feifei]# git config --global user.email "you@example.com"

[root@feigea feifei]# git config --global user.name "Your Name"

[root@feigea feifei]#

[root@feigea feifei]# git commit -m "v1"

[master(根提交) 652632e] v1

 1 file changed, 1 insertion(+)

 create mode 100644 test

 

第六步:查看

 

[root@feigea feifei]# git log

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

步:再次编辑文本

 

[root@feigea feifei]# echo "abc" >> test

 

步:进行添加至暂存区域

 

[root@feigea feifei]# git add .

 

步:提交至仓库

 

[root@feigea feifei]# git commit -m "v2"

[master 671b36e] v2

 1 file changed, 1 insertion(+)

 

步:查看历史版本

 

[root@feigea feifei]# git log

commit 671b36e2937b0c7563b9a10c2c32869a96b052f5

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:19:46 2019 +0800

 

    v2

 

commit 652632e1f585e092641d6df301d1885ac13a09be  #注:此是版本代码,回滚版本时需要引用

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

第十步:回滚版本至v1

 

[root@feigea feifei]# cat test

123

abc

 

第一种方式:

[root@feigea feifei]# git reset --hard 652632e1f585e092641d6df301d1885ac13a09be   #这是v1版本的身份代码

HEAD 现在位于 652632e v1

[root@feigea feifei]# git log

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

[root@feigea feifei]# cat test

123

 

第二种方式:

[root@feigea feifei]# git reset --hard HEAD~1

HEAD 现在位于 652632e v1

[root@feigea feifei]# git log

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

思考:回滚后如何回退到v2版本?】             

 

[root@feigea feifei]# git log

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

第一步:使用git reflog获取commit id

 

[root@feigea feifei]# git reflog   #前面的字符串就是commit id

652632e HEAD@{1}: reset: moving to 652632e1f585e092641d6df301d1885ac13a0

671b36e HEAD@{2}: commit: v2

652632e HEAD@{3}: commit (initial): v1

 

第二步:回退版本至v2

 

[root@feigea feifei]# git reset --hard 671b36e

HEAD 现在位于 671b36e v2

[root@feigea feifei]# cat test

123

abc

 

 

 

:演示撤销各个阶段的文本                                                               

演示1:撤销工作区的内容

 

第一步:编辑已经提交的一个文本

 

[root@feigea feifei]# echo 'cool' >> test

 

第二步:查看文本状态

 

[root@feigea feifei]# 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: test

#

no changes added to commit (use git addand/or git commit -a)

 

第三步:撤销工作区内容

 

[root@feigea feifei]# git checkout --  test

 

第四步:再次查看文本状态

 

[root@feigea feifei]# git status

# On branch master

nothing to commit, working directory clean

 

[root@ken ken]# cat test

123

abc

 

演示2:撤销暂存区的内容

第一步:编辑已经提交的一个文本

 

[root@feigea feifei]# echo 'cool' >> test

 

第二步:提交文件至暂存区

 

——# git add .

 

第三步:查看文本状态

 

——# git status

# On branch master

# Changes to be committed:

# (use git reset HEAD <file>…” to unstage)

#

# modified: test

#

 

第四步:撤销暂存区的文件至工作目录

 

——# git reset HEAD test

Unstaged changes after reset:

M 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: test

#no changes added to commit (use git addand/or git commit -a)

 

第六步:撤销工作区的文本

 

——# git checkout -- test

——# git status

# On branch master

nothing to commit, working directory clean

——# cat test

123

abc

 

九.Git分支介绍                                                         

 

几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。

 

Git 处理分支的方式可谓是难以置信的轻量,创建新分支这一操作几乎能在瞬间完成,并且在不同分支之间的切换操作也是一样便捷。 与许多其它版本控制系统不同,Git 鼓励在工作流程中频繁地使用分支与合并,哪怕一天之内进行许多次。

 

十:Git分支创建和切换                                                 

 

第一步:查看分支

 

[root@feigea feifei]# git branch   #需要在一个已经提交git仓库中才有主分支的概念

* master

 

第二步:创建分支

 

——# git branch feige

——# git branch

feige

* master

 

第三步:切换分支

 

——# git checkout feige

Switched to branch feige’

——# git branch

* feige

master

 

十一:Git分支的使用演示                                                    

 

第一步:在feige分支上面操作文本

 

[root@feigea feifei]# git branch

* feige

  master

[root@feigea feifei]# ls

test

[root@feigea feifei]# cat test

123

abc

[root@feigea feifei]# echo 'cool' >> test

[root@feigea feifei]# cat test

123

abc

cool

 

[root@feigea feifei]# git add .

[root@feigea feifei]# git commit -m 'v3'    #必须提交

[feige e483a8d] v3

 1 file changed, 2 insertions(+)

 

第二步:在feige分支查看版本信息

 

[root@feigea feifei]# git log

commit e483a8d14c3ebc30e99ed3a9fead0d00790d3828

Author: Your Name <you@example.com>

Date:   Mon Jun 17 16:10:48 2019 +0800

 

    v3

 

commit 671b36e2937b0c7563b9a10c2c32869a96b052f5

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:19:46 2019 +0800

 

    v2

 

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

第三步:切换至主分支

 

——# git checkout master

Switched to branch master

——# git branch

feige

* master

 

第四步:查看版本信息

 

[root@feigea feifei]# git log

commit 671b36e2937b0c7563b9a10c2c32869a96b052f5

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:19:46 2019 +0800

 

    v2

 

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

第五步:查看文本信息

 

[root@feigea feifei]# cat test

123

abc

 

第六步:合并分支

 

[root@feigea feifei]# git merge feige

更新 671b36e..e483a8d

Fast-forward

 test | 2 ++

 1 file changed, 2 insertions(+)

 

第七步:再次查看版本信息

 

[root@feigea feifei]# git log

commit e483a8d14c3ebc30e99ed3a9fead0d00790d3828

Author: Your Name <you@example.com>

Date:   Mon Jun 17 16:10:48 2019 +0800

 

    v3

 

commit 671b36e2937b0c7563b9a10c2c32869a96b052f5

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:19:46 2019 +0800

 

    v2

 

commit 652632e1f585e092641d6df301d1885ac13a09be

Author: Your Name <you@example.com>

Date:   Mon Jun 17 15:13:17 2019 +0800

 

    v1

 

第八步:查看文本信息

 

[root@feigea feifei]# cat test

123

abc

cool

 

总结:

 

  1. 在分支上面的开发内容,实际并不会影响我们的主分支

 

  1. 只有在主分支进行合并分支的时候我们才能查看到在子分支上面编辑的内容

 

  1. 这样就大大减轻了我们主分支开发的一个混乱及错误

 

posted @ 2019-06-21 11:08  *云深不知处*  阅读(281)  评论(1编辑  收藏  举报