第三十七课预习任务

1.代码管理平台介绍

2.安装svn

2.1使用yum 安装

2.2 启动svn服务

3.客户端上使用svn(linux)

3.1安装svn

3.2 客户端连接svn服务器

3.3使用svn

4.客户端上使用svn(windows)

5.单机上使用git

5.1安装git

 5.2创建git数据文件,并初始化

5.3 使用git

5.4 git相关操作

6.建立远程仓库

6.1 建立gitlab账号

6.2 将本地仓库推送到远程仓库

7.克隆远程仓库

7.1首先我们在远程github服务器上创建一个test代码文件

7.2在本地linux服务器上克隆远程服务器代码

7.3 本地添加文件推送到服务器上

8.分支管理

9.远程分支管理

10.标签管理

11.git别名

12.搭建git服务器

12.1 创建git用户并创建.ssh文件

12.2 在服务器上创建仓库目录并初始化

12.3在客户机上克隆远程服务器代码

13.安装gitlab

13.1 安装依赖包

13.2 安装 gitlab-ce 社区版 (yum方式,ee是企业版,收费)

13.3、配置并开启 gitlab

14.使用gitlab

15.gitlab备份和恢复


1.代码管理平台介绍

版本控制,记录若干文件内容变化,以便将来查阅特定版本修订情况  版本管理工具发展简史,cvs svn  git  参考http://luckypoem14.github.io/test/2012/04/24/scm-history/  

svn全称subversion,是一个开源版本控制系统,始于2000年  git是linux创始人linus发起的,2005年发布,最初目的是更好管理linux内核代码  git和svn不同在于git不需要依赖服务端就可以工作,即git是分布式的  关于git和svn的比较大家参考http://blog.lishiming.net/?p=305  github是基于git的在线web页面代码托管平台,可以选择付费服务  gitlab可以认为是一个开源的github,两者没有直接关系。

2.安装svn

2.1使用yum 安装

[root@localhost ~]# yum install -y subversion
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                     | 5.9 kB  00:00:00     
 * base: ftp.cuhk.edu.hk
 * epel: epel.mirror.angkasa.id
 * extras: ftp.cuhk.edu.hk
 * updates: ftp.cuhk.edu.hk
..........................................................................................

创建版本库 
[root@localhost ~]# mkdir -p /data/svnroot/myproject
[root@localhost ~]# svnadmin create /data/svnroot/myproject
//编辑auth配置
[root@localhost conf]# vim authz

[groups]
//加入以下文件
admins = user1,user2
[/]
@admins = rw
*= r
[myproject:/]
user1 = rw

//编辑passwd配置
[root@localhost conf]# vim passwd
//为刚刚创建的用户设置密码
[users]
user1 = user1_^^^123
user2 = user2-***123

//编辑svnserve.conf
[root@localhost conf]# vim svnserve.conf
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
realm = /data/svnroot/myproject

2.2 启动svn服务

[root@localhost conf]# svnserve -d -r /data/svnroot
[root@localhost conf]# ps aux |grep svn
root       1233  0.0  0.0 180716   808 ?        Ss   21:38   0:00 svnserve -d -r /data/svnroot
root       1235  0.0  0.0 112704   960 pts/0    R+   21:38   0:00 grep --color=auto svn

3.客户端上使用svn(linux)

3.1安装svn

[root@localhost ~]# yum install -y  subversion
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                     | 5.9 kB  00:00:00     
 * base: mirror.vpshosting.com.hk
 * epel: sg.fedora.ipserverone.com
 * extras: mirror.vpshosting.com.hk
 * updates: mirror.vpshosting.com.hk
.....................................................................................


3.2 客户端连接svn服务器

[root@localhost ~]# svn checkout svn://192.168.139.135/myproject --username=user1
Authentication realm: <svn://192.168.139.135:3690> /data/svnroot/myproject
Password for 'user1': 

-----------------------------------------------------------------------
ATTENTION!  Your password for authentication realm:

   <svn://192.168.139.135:3690> /data/svnroot/myproject

can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes
Checked out revision 0.
[root@localhost ~]# cd myproject 
[root@localhost myproject]# ls -la
total 0
drwxr-xr-x  3 root root  18 Nov 22 21:27 .
dr-xr-x---. 5 root root 200 Nov 22 21:27 ..
drwxr-xr-x  4 root root  75 Nov 22 21:27 .svn

3.3使用svn

//新建一个文件
[root@localhost myproject]# vim 1.txt
//上传到服务器上
[root@localhost myproject]# svn add 1.txt
A         1.txt
[root@localhost myproject]# svn commit -m “1.txt”
Adding         1.txt
Transmitting file data .
Committed revision 1.



[root@localhost myproject]# svn log
------------------------------------------------------------------------
r2 | user1 | 2018-11-22 21:47:56 +0800 (Thu, 22 Nov 2018) | 1 line

“1.txt”
------------------------------------------------------------------------
r1 | user1 | 2018-11-22 21:47:17 +0800 (Thu, 22 Nov 2018) | 1 line

“1.txt”
------------------------------------------------------------------------

4.客户端上使用svn(windows)

  • 官网 https://tortoisesvn.net/index.zh.html  
  • 下载TortoiseSVN 并安装  
  • 简明教程 http://www.jianshu.com/p/6b3b7b915332

5.单机上使用git

5.1安装git

[root@localhost conf]# yum install -y git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.cuhk.edu.hk
 * epel: epel.mirror.angkasa.id
 * extras: ftp.cuhk.edu.hk
 * updates: ftp.cuhk.edu.hk
Resolving Dependencies
........................................................................

 5.2创建git数据文件,并初始化

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd /d
data/ dev/  
[root@localhost ~]# cd /data/gitroot/
[root@localhost gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/
[root@localhost gitroot]# vim 2.txt

5.3 使用git

//把2.txt添加到仓库
[root@localhost gitroot]# git add 2.txt
//add完了必须要commit才算真正把文件提交到git仓库里
[root@localhost gitroot]# git commit -m "add new file 2.txt"
[master (root-commit) 01b6c8a] add new file 2.txt
 Committer: root <[email protected]>
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 [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 3 insertions(+)
 create mode 100644 2.txt



[root@localhost gitroot]# 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:   2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost gitroot]# git diff 2.txt
diff --git a/2.txt b/2.txt
index 5ee294b..b4b7152 100644
--- a/2.txt
+++ b/2.txt
@@ -1,3 +1,3 @@
 kjxxxxxxxxxxxxx..........
 2988888888888
-
+477438477777777

5.4 git相关操作

  • 版本回退
  •  多更改几次2.txt,然后add,commit
  •  git log//查看所有提交记录
  •  git log --pretty=oneline//一行显示
  •  git reset --hard f7c8e9//回退版本,其中后面跟的字符串是简写
  •  撤销修改
  •  rm -f 2.txt//不小心删除了2.txt
  •  git checkout -- 2.txt//恢复2.txt
  •  如果2.txt文件修改,add后但没有commit,再想回退到上一次提交的状态,可以使用git reset HEAD 1.txt,再执行git checkout -- 2.txt
  •  git reflog //查看所有历史版本
  • 删除文件  echo -e "11111111111\n2222222222" > 2.txt  
  • git rm 2.txt  
  • git commit -m "rm 2.txt"

6.建立远程仓库

6.1 建立gitlab账号

首先到 https://github.com 注册一个账号,创建自己的git,点repositories 再点new  名字自定义,比如叫studygit  选择public  点 create repository  添加key:右上角点自己头像,选择settings,左侧选择SSH and GPG keys  左侧点New SSH key,把linux机器上的~/.ssh/id_rsa.pub内容粘贴到这里

6.2 将本地仓库推送到远程仓库


//这一步是在远程创建一个新的仓库studygit,名字尽量和本地的一致
[root@localhost gitroot]# git remote add origin [email protected]:knightlai/gitroot.git

//然后把本地的studygit仓库推送到远程的studygit下一次再推送,就可以直接 git push
[root@localhost gitroot]# git push -u origin master
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5: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.253.113' (RSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/knightlai/gitroot/pull/new/master
remote: 
To [email protected]:knightlai/gitroot.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.



[root@localhost gitroot]# ls
2.txt

 在网站上的github上面也可以看到我们推送上去的本地仓库的内容

7.克隆远程仓库

7.1首先我们在远程github服务器上创建一个test代码文件

7.2在本地linux服务器上克隆远程服务器代码

[root@localhost ~]# git clone  [email protected]:knightlai/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@localhost ~]# ls
anaconda-ks.cfg  test
[root@localhost ~]# cd test
[root@localhost test]# ls
test

7.3 本地添加文件推送到服务器上

//编辑一下代码文件
[root@localhost test]# vim test 
//上传到远程服务器上
[root@localhost test]# git add test
[root@localhost test]# git commit -m "test"
[master 3dc3f3c] test
 Committer: root <[email protected]>
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 [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost test]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To [email protected]:knightlai/test.git
   cf3ea15..3dc3f3c  master -> master

8.分支管理

  • git branch //查看分支  
  • git branch aming  //创建分支  
  • git checkout  test//切换到了test分支下  再用git branch查看,会看到有两个分支master和test,当前使用的分支前面会有一个*在aming分支下 ,编辑2.txt,并提交到新分支  echo "askdfjlksadjflk" >  2.txt  git add 2.txt  git commit -m "laksjdflksjdklfj"  切换回master分支  
  • git checkout master //此时cat 2.txt发现并没有更改内容

 git checkout master //合并分支之前,先切换到目标分支  git merge test   //把test分支合并到了master  如果master分支和test分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。  解决冲突的方法是在master分支下,编辑2.txt,改为aming分支里面2.txt的内容。 然后提交2.txt,再合并aming分支。  但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑2.txt内容,改为想要的,然后提交。切换到test分支,然后合并master分支到test分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。  git  branch -d test //删除分支  

如果分支没有合并,删除之前会提示,那就不合并,强制删除  git branch -D test

  • 对于分支的应用,建议大家以这样的原则来:  master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。  
  • 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master  
  • 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支

9.远程分支管理

  • 本地新建的分支如果不推送到远程,对其他人就是不可见的  
  • 查看远程分支  git ls-remote origin,可以看到所有分支  对于git push分支分两种情况  
  • 当本地分支和远程分支一致时  git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name  
  • 当本地分支比远程分支多,默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name  如果推送失败,先用git pull抓取远程的新提交  
  • git clone的时候默认只把master分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致

10.标签管理

标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态。  

  • git checkout master 先切到master分支上  
  • git tag v1.0  给master打一个标签v1.0  
  • git show v1.0 查看标签信息  
  • git tag 可以查看所有的标签  tag是针对commit来打标签的,所以可以针对历史的commit来打tag  
  • git log --pretty=oneline --abbrev-commit  //先查看历史的commit  
  • git tag v0.9 46d3c1a  //针对历史commit打标签
  •  git tag -a v0.8 -m "tag just v1.1 and so on" 5aacaf4  //可以对标签进行描述  
  • git tag -d v0.8  //删除标签  
  • git push origin v1.0   //推送指定标签到远程  
  • git push --tag origin   //推送所有标签  
  • 如果本地删除了一个标签,远程也想要删除需要这样操作:  
  • git tag v1.0 -d    //删除本地标签  
  • git push origin :refs/tags/v1.0   //删除远程标签

11.git别名

  • git commit 这个命令是不是有点长? 用别名可以提高我们的工作效率  
  • git config --global alias.ci commit  
  • git config --global alias.co  checkout
  •  git config --global alias.br  branch  
  • 查看git别名使用命令  
  • git config --list |grep alias  
  • 查询log小技巧:  git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"  
  • 取消别名  git config --global --unset alias.br

12.搭建git服务器

12.1 创建git用户并创建.ssh文件

[root@localhost test]# useradd -s /usr/bin/git-shell git
[root@localhost test]# cd /home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# touch .ssh/authorized_keys
[root@localhost git]# chown -R git.git .ssh
[root@localhost git]# chmod 600 .ssh/authorized_keys

12.2 在服务器上创建仓库目录并初始化

定好存储git仓库的目录,比如 /data/gitroot
[root@localhost git]# cd /data/gitroot/
//会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
[root@localhost gitroot]# git init --bare sample.git
Initialized empty Git repository in /data/gitroot/sample.git/
//以上操作是在git服务器上做的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的
[root@localhost gitroot]# chown -R git.git sample.git

12.3在客户机上克隆远程服务器代码

[root@localhost ~]# git clone [email protected]:/data/gitroot/sample.git
Cloning into 'sample'...
The authenticity of host '192.168.139.135 (192.168.139.135)' can't be established.
ECDSA key fingerprint is SHA256:oWZvsZbwSgQLmlEi0WFIF+R3I7UIQ0bYV1Yr6+gKNiw.
ECDSA key fingerprint is MD5:74:91:0c:c2:cc:71:31:0a:8a:cc:07:c1:7b:4b:44:0d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.139.135' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.

//在客户端上可以看到从服务器上拉取下来的代码
[root@localhost ~]# ls
anaconda-ks.cfg  myproject  sample
[root@localhost ~]# cd sample/
[root@localhost sample]# ls
[root@localhost sample]# cd myproject
-bash: cd: myproject: No such file or directory
[root@localhost sample]# cd ..
[root@localhost ~]# cd myproject/
[root@localhost myproject]# ls
1.txt  fstab

13.安装gitlab

13.1 安装依赖包

yum install curl policycoreutils openssh-server openssh-clients

systemctl enable sshd  &&  systemctl start sshd

yum install postfix

systemctl enable postfix  &&  systemctl start postfix

firewall-cmd --permanent --add-service=http    //永久设置http服务开放

systemctl reload firewalld

13.2 安装 gitlab-ce 社区版 (yum方式,ee是企业版,收费)

[root@localhost ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh |sudo bash
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
....................................................................................

[root@localhost ~]# yum install gitlab-ce
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.cuhk.edu.hk
 * epel: epel.mirror.angkasa.id
 * extras: ftp.cuhk.edu.hk
 * updates: ftp.cuhk.edu.hk
Resolving Dependencies
--> Running transaction check
.......................................................................................

2、安装 gitlab-ce 社区版(rpm包安装方式)

wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download

rpm -i gitlab-ce-XXX.rpm

13.3、配置并开启 gitlab

gitlab-ctl reconfigure   //此时会输入如下很多信息,并启动好多服务

gitlab-ctl stop/restart/start/status  启动服务/重启服务/停止服务

[root@localhost ~]# gitlab-ctl start
ok: run: alertmanager: (pid 3323) 869s
ok: run: gitaly: (pid 2944) 1206s
ok: run: gitlab-monitor: (pid 3028) 1157s
ok: run: gitlab-workhorse: (pid 2903) 1225s
ok: run: logrotate: (pid 2925) 1218s
ok: run: nginx: (pid 2910) 1225s
ok: run: node-exporter: (pid 3006) 1179s
ok: run: postgres-exporter: (pid 3428) 755s
ok: run: postgresql: (pid 2640) 1306s
ok: run: prometheus: (pid 3148) 1077s
ok: run: redis: (pid 2565) 1313s
ok: run: redis-exporter: (pid 3124) 1114s
ok: run: sidekiq: (pid 2885) 1233s
ok: run: unicorn: (pid 2843) 1239s
[root@localhost ~]# ps aux|grep gitlab
root       1688  1.1  1.3 833180 13280 pts/0    Sl   00:51   0:15 /opt/gitlab/embedded/bin/ruby /opt/gitlab/embedded/bin/chef-client -z -c /opt/gitlab/embedded/cookbooks/solo.rb -j /opt/gitla/embedded/cookbooks/dna.json
root       2543  0.0  0.0   4380    28 ?        Ss   00:51   0:00 runsvdir -P /opt/gitlab/service log: ...........................................................................................................................................................................................................................................................................................................................................................................................................

4、测试访问

首次登陆会跳出设置密码的界面,设置完后自动跳转到登录界面,默认用户名root。

登陆进去后,可以更改用户名、密码等。

初始登入时,总报502,也没有防火墙,经检查是内存不足,我是1G。

5、说明

缺点:这种方式虽然说简单方便,但是定制型很差,默认只能使用postgre和nginx

主配置文件:/etc/gitlab/gitlab.rb   //可以自定义一些邮件服务等

日志地址:/var/log/gitlab/    // 对应各服务

服务地址:/var/opt/gitlab/   // 对应各服务的主目录

仓库地址:/var/opt/gitlab/git-data //记录项目仓库等提交信息

重置配置:gitlab-ctl reconfigure    //不要乱用,会重置为最原始的配置的

重启服务:gitlab-ctl  stop/start/restart  //启动命令

默认安装:postgres、nginx、redis、unicorn ......

6、配置(就是点点点,熟悉熟悉这个应用)

a、创建一个项目组groups,生成路径/var/opt/gitlab/git-data/repositories/;

b、创建一个仓库,可用三种方式链接,新的仓库、已存在的文件夹、已存在的仓库;

创建时可导入 github、gitlab、googlecode 等其他地方的仓库,需要对方token

14.使用gitlab

  • gitlab官网 https://about.gitlab.com/gitlab-com/  
  • 官方安装文档 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)  要求服务器内存不少于2g  
  • vim /etc/yum.repos.d/gitlab.repo//加入如下内容
  • [gitlab-ce] name=Gitlab CE Repository
  • baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/ gpgcheck=0
  • enabled=1  
  • yum install -y gitlab-ce  gitlab-ctl reconfigure

15.gitlab备份和恢复

  • gitlab备份  gitlab-rake gitlab:backup:create  备份目录在/var/opt/gitlab/backups  
  • gitlab 恢复  先停服务 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq  gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀)  
  • 再启动服务 gitlab-ctl start 

注意:我用git add file添加文件时出现这样错误:

fatal: Not a git repository (or any of the parent directories): .git

提示说没有.git这样一个目录,解决办法如下:

git init就可以了!

参考文章:https://www.linuxidc.com/Linux/2017-04/142665.htm

猜你喜欢

转载自blog.csdn.net/a1779078902/article/details/84337147