935  
查询码:00000148
git版本控制系统
作者: wyasw 于 2020年03月29日 发布在分类 / Linux / 服务安装 下,并于 2020年03月29日 编辑
git

  1 版本控制系统

1.1 为什么需要

Git不仅是一款开源的分布式版本控制系统,而且有其独特的功能特性,例如大多数的分布式版本控制系统只会记录每次文件的变化,说白了就是只会关心文件的内容变化差异,而Git则是关注于文件数据整体的变化,直接会将文件提交时的数据保存成快照,而非仅记录差异内容,并且使用SHA-1加密算法保证数据的完整性。

Git为了提高效率,对于没有被修改的文件,则不会重复存储,而是创建一个链接指向之前存储过的文件。

1.2 有了又能怎么样?

l  自动生成备份

l  随时回滚

l  知道改动的地方

l  在正式使用前,我们还需要弄清楚Git的三种重要模式,分别是已提交、已修改、已暂存

Ø  已提交(committed):表示数据文件已经顺利提交到Git数据库中。

Ø  已修改(modified):表示数据文件已经被修改,但未被保存到Git数据库中。

Ø  已暂存(staged):表示数据文件已经被修改,并会在下次提交时提交到Git数据库中。

l  提交前的数据文件可能会被随意修改或丢失,但只要把文件快照顺利提交到Git数据库中,那就可以完全放心了,流程为:

Ø  1.在工作目录中修改数据文件。

Ø  2.将文件的快照放入暂存区域。

Ø  3.将暂存区域的文件快照提交到Git仓库中。


1.3 常见版本管理工具

l  SVN

集中式的版本控制系统,只有一个中央数据仓库,如果中央数据仓库挂了或者不可访问,所有的使用者无法使用SVN,无法进行提交或备份文件。


 

l  Git

分布式的版本控制系统,在每个使用者电脑上就有一个完整的数据仓库,没有网络依然可以使用Git。当然为了习惯及团队协作,会将本地数据同步到Git服务器或者GitHub等代码仓库。


1.4 代码上线

1.4.1 小公司


 

1.4.2 中型公司


 

1.4.3 大型公司


2 git安装配置

Git分布式版本控制系统最佳实践-- http://www.xuliangwei.com/xubusi/385.html

2.1  Git环境准备

[root@centos7-web01 ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[root@centos7-web01 ~]# uname -r

3.10.0-327.el7.x86_64

[root@centos7-web01 ~]# getenforce  ##确认selinx关闭状态

Disabled                  

[root@centos7-web01 ~]# systemctl stop firewalld  ##关闭防火墙

[root@centos7-web01 ~]# ifconfig eno16777736|awk -F '[ ]+' 'NR==2{print $3}'  ##本机ip地址

10.0.0.6

 

2.2  git安装部署

Git是分布式的版本控制系统,我们只要有了一个原始Git版本仓库,就可以让其他主机克隆走这个原始版本仓库,从而使得一个Git版本仓库可以被同时分布到不同的主机之上,并且每台主机的版本库都是一样的,没有主次之分,极大的保证了数据安全性,并使得用户能够自主选择向那个Git服务器推送文件了,其实部署一个git服务器是非常简单的。

[root@centos7-web01 ~]# yum -y install git  ##安装git

[root@centos7-web01 ~]# git config --global user.name "dongshufeng" ##配置使用用户

[root@centos7-web01 ~]# git config --global user.email "dongshufneg1@qq.com"  ##配置git使用邮箱

[root@centos7-web01 ~]# git config --global color.ui true

[root@centos7-web01 ~]# git config --list

user.name=dongshufeng

user.email=dongshufneg1@qq.com

color.ui=true

 

2.3  git常用命令

   add         #添加文件内容至索引

   bisect       #通过二分查找定位引入 bug 的变更

   branch      #列出、创建或删除分支

   checkout    #检出一个分支或路径到工作区

   clone       #克隆一个版本库到一个新目录

   commit     #记录变更到版本库

   diff        #显示提交之间、提交和工作区之间等的差异

   fetch       #从另外一个版本库下载对象和引用

   grep       #输出和模式匹配的行

   init        #创建一个空的 Git 版本库或重新初始化一个已存在的版本库

   log        #显示提交日志

   merge      #合并两个或更多开发历史

   mv        #移动或重命名一个文件、目录或符号链接

   pull       #获取并合并另外的版本库或一个本地分支

   push      #更新远程引用和相关的对象

   rebase     #本地提交转移至更新后的上游分支中

   reset      #重置当前HEAD到指定状态

   rm        #从工作区和索引中删除文件

   show      #显示各种类型的对象

   status      #显示工作区状态

   tag        #创建、列出、删除或校验一个GPG签名的 tag 对象

 

2.4  git基本操作

2.4.1  git提交数据

我们可以简单的把工作目录理解成是一个被Git服务程序管理的目录,Git会时刻的追踪目录内文件的改动,另外在安装好了Git服务程序后,默认就会创建好了一个叫做master的分支,我们直接可以提交数据到了

 

[root@centos7-web01 ~]# mkdir dongshufeng ##创建本地工作目录

[root@centos7-web01 ~]# cd dongshufeng/   ##进入本地工作目录

[root@centos7-web01 dongshufeng]# git init  ##初始为git工作目录

初始化空的 Git 版本库于 /root/dongshufeng/.git/

[root@centos7-web01 dongshufeng]# touch readme.txt ##创建文件

[root@centos7-web01 dongshufeng]# git status  ##查看git状态

位于分支 master

#

初始提交

#

未跟踪的文件:

#   (使用 "git add <file>..." 以包含要提交的内容)

#

#     readme.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

[root@centos7-web01 dongshufeng]# git add readme.txt  ##git添加文件至暂存区

[root@centos7-web01 dongshufeng]# git status

位于分支 master

#

初始提交

#

要提交的变更:

#   (使用 "git rm --cached <file>..." 撤出暂存区)

#

#     新文件:    readme.txt

#

[root@centos7-web01 dongshufeng]# git commit -m "the first commit" ##git commit提交暂存文件至git版本仓库

[master(根提交) a437f84] the first commit

 1 file changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 readme.txt

 

2.4.2  git移除数据

有些时候会向把已经添加到暂存区的文件移除,但仍然希望文件在工作目录中不丢失,换句话说,就是把文件从追踪清单中删除。

 

[root@centos7-web01 dongshufeng]# touch database

[root@centos7-web01 dongshufeng]# git add database

[root@centos7-web01 dongshufeng]# git status

位于分支 master

要提交的变更:

#   (使用 "git reset HEAD <file>..." 撤出暂存区)

#

#     新文件:    database

#

[root@centos7-web01 dongshufeng]# git rm --cached database ##将文件从git暂存区域的列表移除(并不会删除当前目录中的文件)

rm 'database'

[root@centos7-web01 dongshufeng]# git status ##此时文件已经是未追踪状态了

位于分支 master

未跟踪的文件:

#   (使用 "git add <file>..." 以包含要提交的内容)

#

#     database

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

 

####如果想将文件数据从git暂存区和工作目录一起删除,可以做如下操作。

[root@centos7-web01 dongshufeng]# git add database ##再将文件提交到git暂存区

[root@centos7-web01 dongshufeng]# git rm -f database ##但如果在删除之前数据文件已经被放入到暂存区的话,git会担心你误删未提交的文件而报错信息,此时可追加强制删除-f参数。

rm 'database'

[root@centos7-web01 dongshufeng]# ls

readme.txt

[root@centos7-web01 dongshufeng]# git status

位于分支 master

无文件要提交,干净的工作区

 

2.4.3  git移动数据

[root@centos7-web01 dongshufeng]# git mv readme.txt test.txt  ##git如果要修改文件名称,则使用git mv命令。

[root@centos7-web01 dongshufeng]# git status ##查看状态发现下次提交会有一个改名操作。

位于分支 master

要提交的变更:

#   (使用 "git reset HEAD <file>..." 撤出暂存区)

#

#     重命名:    readme.txt -> test.txt

#

[root@centos7-web01 dongshufeng]# git commit -m "changed name" ##提交到git版本仓库

[master 4604c2d] changed name

 1 file changed, 0 insertions(+), 0 deletions(-)

 rename readme.txt => test.txt (100%)

 

####其实可以如下方法改名##

[root@centos7-web01 dongshufeng]# mv test.txt readme.txt  ##先修改名称

[root@centos7-web01 dongshufeng]# git rm test.txt  ##然后删除git版本仓库内的文件快照

rm 'test.txt'

[root@centos7-web01 dongshufeng]# git add readme.txt  ##最后再将新的文件添加进入

[root@centos7-web01 dongshufeng]# git commit -m "changed the file name"  ##提交至git版本仓库

[master 0397676] changed the file name

 1 file changed, 0 insertions(+), 0 deletions(-)

 rename test.txt => readme.txt (100%)

 

2.4.4  git历史记录

[root@centos7-web01 dongshufeng]# git log  ##查看提交历史记录

commit 03976769b56e23332dffbffc59615b1e6516571b

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:42:08 2017 +0800

 

    changed the file name

 

commit 4604c2d7aaa7a12d456a932d72302b28a2e4b9c6

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:37:52 2017 +0800

 

    changed name

 

commit a437f84a7381dfb448d2c20059be0df2660fcbc6

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:12:08 2017 +0800

 

    the first commit

[root@centos7-web01 dongshufeng]# git log -2  ##查看最近两条记录

commit 03976769b56e23332dffbffc59615b1e6516571b

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:42:08 2017 +0800

 

    changed the file name

 

commit 4604c2d7aaa7a12d456a932d72302b28a2e4b9c6

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:37:52 2017 +0800

 

    changed name

 

[root@centos7-web01 dongshufeng]# git log -p -1  ##-p显示每次提交的内容差异,例如仅查看最近一次差异。

commit 03976769b56e23332dffbffc59615b1e6516571b

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:42:08 2017 +0800

 

    changed the file name

 

diff --git a/readme.txt b/readme.txt

new file mode 100644

index 0000000..e69de29

diff --git a/test.txt b/test.txt

deleted file mode 100644

index e69de29..0000000

 

[root@centos7-web01 dongshufeng]# git log --stat -2  ##--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出增减行的概要信息。

commit 03976769b56e23332dffbffc59615b1e6516571b

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:42:08 2017 +0800

 

    changed the file name

 

 readme.txt | 0

 test.txt   | 0

 2 files changed, 0 insertions(+), 0 deletions(-)

 

commit 4604c2d7aaa7a12d456a932d72302b28a2e4b9c6

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 02:37:52 2017 +0800

 

    changed name

 

 readme.txt | 0

 test.txt   | 0

 2 files changed, 0 insertions(+), 0 deletions(-)

 

[root@centos7-web01 dongshufeng]# git log --pretty=oneline  ##--pretty根据不同的格式展示提交的历史信息。

03976769b56e23332dffbffc59615b1e6516571b changed the file name

4604c2d7aaa7a12d456a932d72302b28a2e4b9c6 changed name

a437f84a7381dfb448d2c20059be0df2660fcbc6 the first commit

[root@centos7-web01 dongshufeng]# git log --pretty=fuller -2  ##以更详细的模式输出提交的历史记录

commit 03976769b56e23332dffbffc59615b1e6516571b

Author:     dongshufeng <dongshufneg1@qq.com>

AuthorDate: Sat Jul 29 02:42:08 2017 +0800

Commit:     dongshufeng <dongshufneg1@qq.com>

CommitDate: Sat Jul 29 02:42:08 2017 +0800

 

    changed the file name

 

commit 4604c2d7aaa7a12d456a932d72302b28a2e4b9c6

Author:     dongshufeng <dongshufneg1@qq.com>

AuthorDate: Sat Jul 29 02:37:52 2017 +0800

Commit:     dongshufeng <dongshufneg1@qq.com>

CommitDate: Sat Jul 29 02:37:52 2017 +0800

 

    changed name

 

还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有:

%s  提交说明。

%cd 提交日期。

%an 作者的名字。

%cn 提交者的姓名。

%ce 提交者的电子邮件。

%H  提交对象的完整SHA-1哈希字串。

%h  提交对象的简短SHA-1哈希字串。

%T  树对象的完整SHA-1哈希字串。

%t  树对象的简短SHA-1哈希字串。

%P  父对象的完整SHA-1哈希字串。

%p  父对象的简短SHA-1哈希字串。

%ad 作者的修订时间。

[root@centos7-web01 dongshufeng]# git log --pretty=fomat:"%h %cn"  ##查看当前所有提交记录的简短SHA-1哈希字串与提交者的姓名

fomat:0397676 dongshufeng

fomat:4604c2d dongshufeng

fomat:a437f84 dongshufeng

 

2.4.5  git还原数据

[root@centos7-web01 dongshufeng]# echo "git is a version control system" >> readme.txt  ##追加一段话

[root@centos7-web01 dongshufeng]# git add readme.txt

[root@centos7-web01 dongshufeng]# git commit -m "introduction software"

[master fadb41b] introduction software

 1 file changed, 1 insertion(+)

 

此时觉得写得不对,想还原某一次提交的文件快照

[root@centos7-web01 dongshufeng]# git log --pretty=oneline  ##提交的历史信息

fadb41b94d9a97b21c9cf2a7352396148df94cbe introduction software

03976769b56e23332dffbffc59615b1e6516571b changed the file name

4604c2d7aaa7a12d456a932d72302b28a2e4b9c6 changed name

a437f84a7381dfb448d2c20059be0df2660fcbc6 the first commit

 

       git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交记录,而上一个提交版本会叫HEAD^,上上一个版本叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。

[root@centos7-web01 dongshufeng]# cat readme.txt

git is a version control system

[root@centos7-web01 dongshufeng]# git reset --hard HEAD^  ##还原历史提交版本上一次

HEAD 现在位于 0397676 changed the file name

[root@centos7-web01 dongshufeng]# cat readme.txt  ##查看文件内容已经还原

[root@centos7-web01 dongshufeng]#

 

刚才的操作实际上就是改变了一下HEAD版本指针的位置,就是你将HEAD指针放在那里,那么你的当前工作版本就会定位在那里,要想把内容再还原到最新的提交的版本,先查看下提交版本号。

[root@centos7-web01 dongshufeng]# git log --pretty=oneline

03976769b56e23332dffbffc59615b1e6516571b changed the file name

4604c2d7aaa7a12d456a932d72302b28a2e4b9c6 changed name

a437f84a7381dfb448d2c20059be0df2660fcbc6 the first commit

  怎么搞的?竟然没有了introduction software这个提交的版本记录?

  原因很简单,因为我们当前的工作版本是历史的一个提交点,这个历史提交点还没有发生过introduction software更新记录,所以当然就看不到了,要是想还原到未来的历史更新点,可以用git reflog命令来查看所有的历史记录:

[root@centos7-web01 dongshufeng]# git reflog  ##查看未来历史更新点

0397676 HEAD@{0}: reset: moving to HEAD^

fadb41b HEAD@{1}: commit: introduction software

0397676 HEAD@{2}: commit: changed the file name

4604c2d HEAD@{3}: commit: changed name

a437f84 HEAD@{4}: commit (initial): the first commit

 

[root@centos7-web01 dongshufeng]# git reset --hard fadb41b  ##找到历史还原点SHA-1值后,就可以还原(值不写全,系统会自动匹配)。

HEAD 现在位于 fadb41b introduction software

[root@centos7-web01 dongshufeng]# cat readme.txt ##查看文件内容:已经还原。

git is a version control system

 

  如果只是想把某个文件内容还原,就不必这么麻烦,直接用git checkout命令就可以的,先写一段话

[root@centos7-web01 dongshufeng]# echo "Some mistkes words" >> readme.txt

[root@centos7-web01 dongshufeng]# cat readme.txt

git is a version control system

Some mistkes words

 

  我们突然发现不应该写这一句话的,可以手工删除(当内容比较多的时候会很麻烦),还可以将文件内容从暂存区中恢复

[root@centos7-web01 dongshufeng]# git checkout -- readme.txt

[root@centos7-web01 dongshufeng]# cat readme.txt

git is a version control system

  这其中有一套规则,如果暂存区中有该文件,则直接从暂存区恢复,如果暂存区没有该文件,则将还原成最近一次文件提交时的快照。

 

2.4.5.1  对比数据

git diff可以对比当前文件与仓库已保存文件的区别,知道了对README作了什么修改

后,再把它提交到仓库就放⼼多了。

git diff readme.txt

 

2.4.6  git管理分支结构

  分支即是平行空间,假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到git仓库中,又有可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫“拍照功能”的分支,这种分支只会属于你自己,而其他人看不到,等代码编写完成后再与原来的项目主分支合并下即可,这样即能保证代码不丢失,又不影响其他人的工作。


一般在实际的项目开发中,我们要尽量保证master分支是非常稳定的,仅用于发布新版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后在合并到master分支上面,所以团队的合作分支看起来会像上面图那样。


 

2.4.6.1   git创建分支

[root@centos7-web01 dongshufeng]# git branch linux  ##创建爱你分支

[root@centos7-web01 dongshufeng]# git checkout linux  ##切换分支

切换到分支 'linux'

[root@centos7-web01 dongshufeng]# git branch  ##查看当前分支情况,当前分支有*

* linux

  master

[root@centos7-web01 dongshufeng]# echo "Create new branch is linux" >> readme.txt ##对文件追加一行内容。

[root@centos7-web01 dongshufeng]# git add readme.txt

[root@centos7-web01 dongshufeng]# git commit -m "new branch"

[linux ca82097] new branch

 1 file changed, 1 insertion(+)

[root@centos7-web01 dongshufeng]# git checkout master  ##提交文件后再切回master分支

切换到分支 'master'

[root@centos7-web01 dongshufeng]# cat readme.txt   ##查看文件内容,发现并没有新追加的内容

git is a version control system

 

2.4.6.2   git合并分支

  现在,我们想把linux的工作成果合并到master分支上,则可以使用“git merge”命令来将指定的分支与当前分支合并


[root@centos7-web01 dongshufeng]# git branch ##查看在maser分支上

  linux

* master

[root@centos7-web01 dongshufeng]# git merge linux  ##合并Linux分支到master

更新 fadb41b..ca82097

Fast-forward

 readme.txt | 1 +

 1 file changed, 1 insertion(+)

[root@centos7-web01 dongshufeng]# cat readme.txt ##查看合并后的文件

git is a version control system

Create new branch is linux

[root@centos7-web01 dongshufeng]# git branch -d linux ##确认合并完成后,可以放心的删除linux分支。

已删除分支 linux(曾为 ca82097)。

[root@centos7-web01 dongshufeng]# git branch ##删除后,查看branch,只剩下master分支了。

* master

 

2.4.6.3   git分支冲突

  但是Git并不能每次都为我们自动的合并分支,当遇到了内容冲突比较复杂的情况,则必须手工将差异内容处理点,比如这样的情况:


[root@centos7-web01 dongshufeng]# git checkout -b linux  ##创建分支并切换到该分支

切换到一个新分支 'linux'

[root@centos7-web01 dongshufeng]# git branch  ##查看分支

* linux

  master

[root@centos7-web01 dongshufeng]# vim readme.txt  ##编辑readme文件

[root@centos7-web01 dongshufeng]# cat readme.txt

git is a version control system

Create new branch is linux

linux

linux

linux

[root@centos7-web01 dongshufeng]# git add readme.txt  ##linux分支添加readme至暂存区

[root@centos7-web01 dongshufeng]# git commit -m "create two"  ##linux分支提交readme

[linux 65be9c6] create two

 1 file changed, 3 insertions(+)

[root@centos7-web01 dongshufeng]# git checkout master  ##切换到master分支

切换到分支 'master'

[root@centos7-web01 dongshufeng]# git branch ##查看是否切换到master分支

  linux

* master

[root@centos7-web01 dongshufeng]# vim readme.txt ##编在master分支上修改readme文件同一行内容

[root@centos7-web01 dongshufeng]# git add readme.txt  ##添加至暂存区

[root@centos7-web01 dongshufeng]# git commit -m "create to master"  ##提交至git版本仓库

[master 687c316] create to master

 1 file changed, 3 insertions(+)

[root@centos7-web01 dongshufeng]# git merge linux   ##合并linux分支(冲突)

自动合并 readme.txt

冲突(内容):合并冲突于 readme.txt

自动合并失败,修正冲突然后提交修正的结果。

 

####那么此时,我们在masterlinux分支上都分别对中readme文件进行了修改并提交了,那这种情况下Git就没法再为我们自动的快速合并了,它只能告诉我们readme文件的内容有冲突,需要手工处理冲突的内容后才能继续合并

[root@centos7-web01 dongshufeng]# cat readme.txt ##冲突内容如下

##Git< <<<<<<=======>>>>>>>分割开了各个分支冲突的内容,我们需要手工的删除这些符号,并将内容修改。

<<<<<<< HEAD

1linux 2linux 3linux

=======

1linux 2linux

>>>>>>> linux

git is a version control system

Create new branch is linux

[root@centos7-web01 dongshufeng]# vim readme.txt ##编辑手工删除那些符号,并将内容修改

[root@centos7-web01 dongshufeng]# git add readme.txt

[root@centos7-web01 dongshufeng]# git commit -m "config"

[master 65036ca] config

 1 file changed, 1 deletion(-)

[root@centos7-web01 dongshufeng]# git branch -d linux ##最后删除linux分支

已删除分支 linux(曾为 4e89693)。

[root@centos7-web01 dongshufeng]# git branch  ##检查是否删除完毕

* master

 

2.4.7  git管理标签

当版本仓库内的数据有个大的改善或者功能更新,我们经常会打一个类似于软件版本号的标签,这样通过标签就可以将版本库中的某个历史版本给记录下来,方便我们随时将特定历史时期的数据取出来用,另外打标签其实只是像某个历史版本做了一个指针,所以一般都是瞬间完成的。

[root@centos7-web01 ~]# cd dongshufeng/  ##进入git版本控制系统

[root@centos7-web01 dongshufeng]# git tag v1.0  ##当前提交内容打一个标签(方便快速回滚)

[root@centos7-web01 dongshufeng]# git tag  ##查看当前所有的标签

v1.0

[root@centos7-web01 dongshufeng]# git show v1.0  ##查看当前1.0版本的详细信息

commit 65036ca8c398c2e23839bd11f8e40e8d11365bec

Author: dongshufeng <dongshufneg1@qq.com>

Date:   Sat Jul 29 07:04:26 2017 +0800

 

    config

 

diff --git a/readme.txt b/readme.txt

index f3e43de..621186b 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1,4 +1,3 @@

 1linux 2linux

-linux

 git is a version control system

 Create new branch is linux

[root@centos7-web01 dongshufeng]# git tag v1.1 -m "version 1.1 release is test" ##创建带有说明的标签,-a指定标签名字,-m指定说明文字

[root@centos7-web01 dongshufeng]# git tag -d v1.0 ##我们为同一个提交版本设置了两次标签,删除之前的v1.0.

已删除 tag 'v1.0'(曾为 65036ca

[root@centos7-web01 dongshufeng]# git tag  ##再次查看,v1.0已经被删除。

v1.1

 

gitlab安装部署

3.1 安装和配置必要的依赖关系Install and configure the necessary dependencies

如果你安装postfix发送邮件,请选择“网站设置”中。而不是使用后缀也可以用邮件或 配置自定义SMTP服务器。如果你想使用的进出口,请 配置为SMTP服务器。

CentOS7,下面的命令将在系统防火墙打开HTTPSSH访问。

yum install -y curl openssh-server postfix

systemctl enable sshd postfix

systemctl start sshd postfix

firewall-cmd --permanent --add-service=http

systemctl reload firewalld

 

3.2 添加gitlab包服务器安装包Add the GitLab package server and install the package

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh| sudo bash

yum install -y gitlab-ce

因为使用国外的源比较慢;建议使用国内的源下载之后,再安装

gitlab清华源URL

https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

wget https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-9.4.2-ce.0.el7.x86_64.rpm

yum localinstall -y gitlab-ce-9.4.2-ce.0.el7.x86_64.rpm


3.3 配置并启动gitlab Configure and start GitLab

[root@centos7-web01 ~]# gitlab-ctl reconfigure

这里需要注意的是,内存给大一点,最少1G,不然在配置的时候会出现报错,还有就是如果打开界面出现502,请检查808080端口是否被占用。

使用gitlab-ctl管理gitlab,例如: *查看gitlab状态

[root@centos7-web01 ~]# gitlab-ctl status

run: gitaly: (pid 18243) 68s; run: log: (pid 17984) 152s

run: gitlab-monitor: (pid 18287) 66s; run: log: (pid 18141) 106s

run: gitlab-workhorse: (pid 18253) 68s; run: log: (pid 17999) 146s

run: logrotate: (pid 18028) 138s; run: log: (pid 18027) 138s

run: nginx: (pid 18011) 144s; run: log: (pid 18010) 144s

run: node-exporter: (pid 18083) 125s; run: log: (pid 18082) 125s

run: postgres-exporter: (pid 18276) 66s; run: log: (pid 18124) 113s

run: postgresql: (pid 17806) 210s; run: log: (pid 17805) 210s

run: prometheus: (pid 18264) 67s; run: log: (pid 18062) 131s

run: redis: (pid 17745) 216s; run: log: (pid 17744) 216s

run: redis-exporter: (pid 18098) 119s; run: log: (pid 18097) 119s

run: sidekiq: (pid 17968) 158s; run: log: (pid 17967) 158s

run: unicorn: (pid 17930) 164s; run: log: (pid 17929) 164s

 

关闭gitlab

[root@centos7-web01 ~]# gitlab-ctl stop

ok: down: gitaly: 0s, normally up

ok: down: gitlab-monitor: 1s, normally up

ok: down: gitlab-workhorse: 0s, normally up

ok: down: logrotate: 0s, normally up

ok: down: nginx: 1s, normally up

ok: down: node-exporter: 0s, normally up

ok: down: postgres-exporter: 1s, normally up

ok: down: postgresql: 0s, normally up

ok: down: prometheus: 1s, normally up

ok: down: redis: 0s, normally up

ok: down: redis-exporter: 0s, normally up

ok: down: sidekiq: 0s, normally up

ok: down: unicorn: 1s, normally up

 

启动 gitlab

[root@centos7-web01 ~]# gitlab-ctl start

ok: run: gitaly: (pid 18487) 1s

ok: run: gitlab-monitor: (pid 18491) 0s

ok: run: gitlab-workhorse: (pid 18494) 1s

ok: run: logrotate: (pid 18501) 0s

ok: run: nginx: (pid 18507) 0s

ok: run: node-exporter: (pid 18512) 1s

ok: run: postgres-exporter: (pid 18516) 0s

ok: run: postgresql: (pid 18520) 1s

ok: run: prometheus: (pid 18528) 0s

ok: run: redis: (pid 18535) 1s

ok: run: redis-exporter: (pid 18539) 0s

ok: run: sidekiq: (pid 18543) 0s

ok: run: unicorn: (pid 18550) 1s

 

重启服务

[root@centos7-web01 ~]#  gitlab-ctl restart

ok: run: gitaly: (pid 18587) 1s

ok: run: gitlab-monitor: (pid 18591) 0s

ok: run: gitlab-workhorse: (pid 18594) 0s

ok: run: logrotate: (pid 18603) 1s

ok: run: nginx: (pid 18609) 0s

ok: run: node-exporter: (pid 18614) 1s

ok: run: postgres-exporter: (pid 18618) 0s

ok: run: postgresql: (pid 18627) 0s

ok: run: prometheus: (pid 18635) 0s

ok: run: redis: (pid 18637) 0s

ok: run: redis-exporter: (pid 18644) 0s

ok: run: sidekiq: (pid 18650) 1s

ok: run: unicorn: (pid 18658) 0s

 

3.4 浏览到主机名和登录Browse to the hostname and login

输入IP地址,默认是root用户,提示让你输入密码,不能过于简单。 使用root用户和刚才创建的密码登录后,就可以自己玩了。

3.5 取消用户注册

gitlab记得要取消用户注册,访问下面网址,去掉”Sign-up enabled” ,位置在 http://10.0.0.6/admin/application_settings

最后别忘了点保存。

  3.6  新增用户 dongshufeng( 这里需要注意的就是在创建完以后退出用 dongshufeng 用户登录时,第一次需要更改密码 )

登录邮箱查看

3.7  gitlab配置ssh连接

ssh公钥登录


ssh公钥登录

一般在管理远程主机时,都用ssh登录,ssh user@host,但是这样每次会使用密码。 使用ssh-keygen生成的密钥对,然后将公钥添加的目标主机的~/.ssh/authorized_keys文件中,当前主机就成为可信任的主机,下次使用ssh登录时,就不用输入密码了。

GitlabGithub都支持这种方式的连接,具体操作步骤如下:

一、生成秘钥对

[root@centos7-web01 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

/root/.ssh/id_rsa already exists.

Overwrite (y/n)? y

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

57:18:4e:09:e6:0c:b1:c6:9a:f7:5a:8a:79:1f:6c:09 root@centos7-web01

The key's randomart image is:

+--[ RSA 2048]----+

|      o.o.o.     |

|     . * o.o     |

|      + o o .    |

|     +     .     |

|    o E S .      |

|     . + o       |

|        B        |

|     o.= .       |

|    o.o..        |

+-----------------+

 

##就在主目录下的.ssh目录中生成了两个文件id_rsaid_rsa.pubid_rsa中保存的是私钥,id_rsa.pub中保存的是公钥。

二、 添加公钥

[root@centos7-web01 ~]# cd .ssh/

[root@centos7-web01 .ssh]# ls

id_rsa  id_rsa.pub

[root@centos7-web01 .ssh]# cat id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrSOLdtF2+Ryo07YC+ZHbGGCje1GzRvckjomcy/ysb1zLNubRVejvAXOtrew7g7EKGS69ZY0VVEXHukVKPoE9i0avxzNRQCdiahsJRHcolBe5Aw84Xp3CPWl1EWqYlVEiGwwTJhw/JJJ6cD1JI5kwLPYw/HTb2piwms4iCN2aDIXvYOX3mXxEReasD64jQTW2HNtBf474pa3mv0Qf0gA27p6ATFCYIwa7OKuledKA2dG2WEmNz+gBG2cnQBGeH/ViqnyHGetnoxnqS0c7kmcsQScYZ06xKPnTiBs1tjBq1xCRFjejPkPKGIK4IXwuEBxsGn9FPYfFESAnUoIW2HyqT root@centos7-web01


在 个人资料->SSH Keys->Add new 页面中粘贴公钥,就添加完成了

添加git服务器上生成的公钥id_rsa.pub点击Add  key添加

查看ssh公钥信息


三、测试

ssh 加 -T选项测试目标服务是否可用

[root@centos7-web01 .ssh]# ssh -T git@"10.0.0.6"     ##"里面填写你的gitlab服务器地址"

The authenticity of host '10.0.0.6 (10.0.0.6)' can't be established.

ECDSA key fingerprint is ee:ec:5e:57:6f:17:33:6b:64:de:25:73:6b:b8:45:5c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '10.0.0.6' (ECDSA) to the list of known hosts.

Welcome to GitLab, dong!

第一次连接时,会询问是否信任主机,确认后输入yes。如果看到Welcome to GitLab, Rusher!就算配置成功了,接下来就可以通过ssh来提交代码了。

四、 Widows操作

1.        下载 Git-Bash

2.        生成密钥对ssh-keygen -t rsa -C "你的邮箱"

3.        生成之后用 notepad c:/User/Administrator/.ssh/id_rsa.pub 打开文件,然后将公钥添加的Gitlab.

4.        测试 ssh -T git@"你的gitlab服务器地址"


准备工作已经完毕,右上角点击创建一个新的仓库

3.8 创建组和项目并管理用户到组

3.9 添加分支,并提交代码

##当前位置

[root@centos7-web01 linux]# cd linux-project/

[root@centos7-web01 linux-project]# ls

README.md

[root@centos7-web01 linux-project]# pwd

/source/linux/linux-project

##创建分支dev

[root@centos7-web01 linux-project]# git branch dev

##切换到dev分支

[root@centos7-web01 linux-project]# git checkout dev

切换到分支 'dev'

##推送到gitlab

[root@centos7-web01 linux-project]# git push origin dev

Username for 'http://10.0.0.6': dong

Password for 'http://dong@10.0.0.6':

Total 0 (delta 0), reused 0 (delta 0)

remote:

remote: To create a merge request for dev, visit:

remote:   http://localhost/linux/linux-project/merge_requests/new?merge_request%5Bsource_branch%5D=dev

remote:

To http://10.0.0.6/linux/linux-project.git

 * [new branch]      dev -> dev

##修改部分内容

[root@centos7-web01 linux-project]# vim README.md

[root@centos7-web01 linux-project]# cat README.md

这只是测试使用

分支测试

##推送之前需要做下面操作

[root@centos7-web01 linux-project]# git config user.name "dong"

[root@centos7-web01 linux-project]# git config --global user.email "21325307@qq.com"

[root@centos7-web01 linux-project]# git add *

[root@centos7-web01 linux-project]# git commit -m "ceshi"

[dev f12db55] ceshi

 1 file changed, 2 insertions(+), 1 deletion(-)

##推送到分支dev

[root@centos7-web01 linux-project]# git push -u origin dev

Username for 'http://10.0.0.6': dong

Password for 'http://dong@10.0.0.6':

Counting objects: 5, done.

Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

remote:

remote: To create a merge request for dev, visit:

remote:   http://localhost/linux/linux-project/merge_requests/new?merge_request%5Bsource_branch%5D=dev

remote:

To http://10.0.0.6/linux/linux-project.git

   e4b3531..f12db55  dev -> dev

分支 dev 设置为跟踪来自 origin 的远程分支 dev

[root@centos7-web01 linux-project]#

执行完操作会发现在gitlab上面多了一个dev分支。

3.10  git客户端工具

https://www.sourcetreeapp.com/

GitMercurialWindowsMac的客户端

GitHub托管服务

Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓库,还拥有了更美观的Web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业。

大多数用户都是为了寻找资源而爱上Github的,首先进入网站,点击注册(Sign up)

 

4.1 没有注册的点击注册:

4.2 填写注册信息

4.3 选择仓库类型,默认免费,点击底下finish sign up注册:

4.4 我们在向Github推送文件时,可以选择SSH协议模式,在本机生成密钥

[root@dsf ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

30:df:d0:9e:13:21:f3:bc:a1:83:6e:62:8e:bf:02:c7 root@dsf

The key's randomart image is:

+--[ RSA 2048]----+

|        o .      |

|         * .     |

|      o . *      |

|       = = =     |

| .    . S *      |

|. E  .   . .     |

| o  o o          |

|  .+ o           |

|  .o+.           |

+-----------------+

[root@dsf ~]# cat ~/.ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA2BbpaAPUGuBDKwKezD4419kmZZB+3tuxb8xxCoJvuZoR4GtmVQuVGxuSsyP6emQzWFbaifTMq3ryqDIEO9LFnaZ20sGzSc9ywUqzyd94wvD6R2ne+b48sRKRtv2gCGisCUsUfc0aYkHq8VM91FT3KicXCQt3QPNCKUHbvF5jViRqFkcm9W7VB2e0qpwTHqHBo17urAKQvsQ7thCsi3s//XNlwMJ1KROG7w7RCWJcTB0IbfNoyadW4sdiVMHBuHb4/QgTh6na5r+zSoRmU5c9qp/cGSBMaoZjSngltWMxIJiCzwOdcgqAwm7xhJ98e1aOsoky5xq98hvrX57heueQMQ== root@dsf

 

点击setting账户配置

4.5 准备工作已经完毕,右上角点击创建一个新的仓库

4.6 创建成功后会跳转到该仓库,选择ssh模式,复制版本仓库克隆地址

 

[root@dsf ~]# git clone git@github.com:dongshufneg1/demo.git  ##手动克隆测试

Initialized empty Git repository in /root/demo/.git/

The authenticity of host 'github.com (192.30.255.113)' can't be established.

RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.

remote: Counting objects: 3, done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Receiving objects: 100% (3/3), done.

[root@dsf ~]# cd demo/

[root@dsf demo]# ls

README.md

[root@dsf demo]# cat README.md  ##查看详细信息

# demo

描述信息-可以不写

[root@dsf demo]# echo "test is git">>test.txt   ##添加新文件

[root@dsf demo]# git add test.txt   ##添加到git暂存区

[root@dsf demo]# git commit -m "add test file"  ##提交至git版本仓库

[master fd37bb4] add test file

 Committer: root <root@dsf.(none)>

Your name and email address were configured automatically based

on your username and hostname. Please check that they are accurate.

You can suppress this message by setting them explicitly:

 

    git config --global user.name "Your Name"

    git config --global user.email you@example.com

 

If the identity used for this commit is wrong, you can fix it with:

 

    git commit --amend --author='Your Name <you@example.com>'

 

 1 files changed, 1 insertions(+), 0 deletions(-)

 create mode 100644 test.txt

[root@dsf demo]# git remote  ##查看本机列表

origin

[root@dsf demo]# git push -u origin master  ##然后将本地的git仓库同步远程到github服务器上(第一次请加上参数-u,代表关联本地与远程)。

Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.

Counting objects: 4, done.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 278 bytes, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@github.com:dongshufneg1/demo.git

   1d3b615..fd37bb4  master -> master

Branch master set up to track remote branch master from origin.

4.7 刷新一下web,果然看到版本仓库已经同步了

参  考

Git Help

https://help.github.com/articles/generating-ssh-keys

 

鸡血哥的blog---gitlab-简单安装及使用

http://www.jixuege.com/blog/2016/08/10/CI-gitlab-%E7%AE%80%E5%8D%95%E5%AE%89%E8%A3%85%E5%8F%8A%E4%BD%BF%E7%94%A8-1/

git 汉化:

http://www.jixuege.com/blog/2016/09/21/gitlab-to-convert-sth-into-Chinese/ 

 

徐亮偉架构师之路的博客---Git分布式控制系统最佳实践

http://www.xuliangwei.com/xubusi/385.html

 

老鹰之歌的学习笔记博客---gitlab配置ssh连接

http://blog.csdn.net/black_ox/article/details/17753943

 



 推荐知识

 历史版本

修改日期 修改人 备注
2020-03-29 20:50:58[当前版本] wyasw 创建版本

 附件

附件类型

JPGJPG

文艺知识分享平台 -V 4.9.5 -wcp
京公网安备100012199188号 京ICP备2021030911号