博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码管理平台
阅读量:7242 次
发布时间:2019-06-29

本文共 30198 字,大约阅读时间需要 100 分钟。

  hot3.png

一、代码管理平台介绍

版本控制,记录若干文件内容变化,以便将来查阅特定版本修订情况

版本管理工具发展史:cvs-->svn-->git

svn:全称subversion,是一个开源版本控制系统,始于2000年

git是Linux的创始人linus发起的,2005年发布。

git与svn不同在于git不需要依赖服务端就可以工作,即git是分布式的。

githup是基于git的在线web页面代码托管平台,可以选择付费服务。

gitlab可以认为是一个开源的github,两种没有直接关系。

二、安装svn

rhel7.5系统

1、使用yum安装svn

[root@node1 ~]# yum install -y subversion

2、创建版本库

[root@node1 ~]# mkdir -p /data/svnroot/myproject[root@node1 ~]# svnadmin create /data/svnroot/myproject/[root@node1 ~]# ls /data/svnroot/myproject/conf  db  format  hooks  locks  README.txt[root@node1 ~]#

3、修改配置文件

修改权限文件:

[root@node1 ~]# cd /data/svnroot/myproject/conf/[root@node1 conf]# vim authz[groups][/]@admins = rw* = r[myproject:/]user1 = rw

修改密码文件:

[root@node1 conf]# vim passwd[users]user1 = 123456

编辑配置文件:

[root@node1 conf]# vim svnserve.conf[general]anon-access = noneauth-access =  writepassword-db = passwdauthz-db = authzrealm = /data/svnroot/myproject

4、启动svn

[root@node1 ~]# svnserve -d -r /data/svnroot/[root@node1 ~]# ps aux | grep svnroot      10581  0.0  0.0 180712   808 ?        Ss   22:56   0:00 svnserve -d -r /data/svnroot/root      10765  0.0  0.0 112704   968 pts/0    S+   22:57   0:00 grep --color=auto svn[root@node1 ~]#

三、Linux上使用svn

在node2上使用svn

1、先安装svn:

[root@node2 ~]# yum install -y subversion

2、连接

[root@node2 ~]# svn checkout svn://192.168.10.205/myprojectAuthentication realm: 
/data/svnroot/myprojectPassword for 'root': Authentication realm:
/data/svnroot/myprojectUsername: user1Password for 'user1': -----------------------------------------------------------------------ATTENTION! Your password for authentication realm:
/data/svnroot/myprojectcan only be stored to disk unencrypted! You are advised to configureyour system so that Subversion can store passwords encrypted, ifpossible. See the documentation for details.You can avoid future appearances of this warning by setting the valueof the 'store-plaintext-passwords' option to either 'yes' or 'no' in'/root/.subversion/servers'.-----------------------------------------------------------------------Store password unencrypted (yes/no)? yesChecked out revision 0.[root@node2 ~]# cd myproject[root@node2 myproject]# ls -latotal 4drwxr-xr-x. 3 root root 18 Jul 31 23:06 .dr-xr-x---. 10 root root 4096 Jul 31 23:06 ..drwxr-xr-x. 4 root root 75 Jul 31 23:06 .svn[root@node2 myproject]#

连接成功。

或者:

[root@node2 ~]# svn checkout svn://192.168.10.205/myproject --username=user1Checked out revision 0.[root@node2 ~]#

进入项目:

[root@node2 ~]# cd myproject/[root@node2 myproject]# ls[root@node2 myproject]# ls -latotal 4drwxr-xr-x.  3 root root   18 Jul 31 23:06 .dr-xr-x---. 10 root root 4096 Jul 31 23:06 ..drwxr-xr-x.  4 root root   75 Jul 31 23:09 .svn[root@node2 myproject]#

测试:将本地的文件传到服务端。

[root@lb02 myproject]# cp /etc/fstab .[root@lb02 myproject]# svn add ./fstab A         fstab[root@lb02 myproject]# [root@lb02 myproject]# svn commit -m "add fstab"Adding         fstabTransmitting file data .Committed revision 1.[root@lb02 myproject]#

复制/etc/fstab文件文件到myproject中,然后使用svn add命令,最后使用svn commit -m命令上传。

到服务端查看一下,是否成功。

删除文件:

本地删除:

[root@lb02 myproject]# svn delete hahaD         haha[root@lb02 myproject]# svn delete fstab D         fstab[root@lb02 myproject]#

在服务器上删除:

[root@lb02 myproject]# svn commit -m "delete fstab"[root@lb02 myproject]#

四、客户端Windows上使用svn

安装TortoiseSVN

下载地址:https://tortoisesvn.net/index.zh.html

下载安装完成。

创建文件夹:H:\myproject

鼠标右键选择SVN 检出

18a8333b3e2cb07b34ba35e5455e4a324af.jpg

确定,输入用户名密码即可。

进入H:\myproject创建一个文件

比如123.txt,右键选择添加,然后右键选择提交:

2358b5c2f0f658a3e8ca71f4db32a02bb64.jpg

信息填写,add 123.txt,然后确定

e61c7c16aec3f64e6246b6b5059d6d1f38c.jpg

登录Linux:

[root@lb02 myproject]# svn upUpdating '.':A    123.txtUpdated to revision 4.[root@lb02 myproject]# ls123.txt[root@lb02 myproject]#

OK,更新了。

五、单机上使用git

1、安装git

[root@lb01 ~]# yum install -y git

2、创建目录

[root@lb01 ~]# mkdir /data/gitroot

3、初始化仓库

[root@lb01 ~]# cd /data/gitroot/[root@lb01 gitroot]# git initInitialized empty Git repository in /data/gitroot/.git/[root@lb01 gitroot]#

4、测试

[root@lb01 gitroot]# echo 123 > 1.txt[root@lb01 gitroot]# git add 1.txt[root@lb01 gitroot]# git commit -m "add 1.txt"*** 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@lb01.(none)')[root@lb01 gitroot]#

报错,需要邮箱,用户名:

[root@lb01 gitroot]# git config --global user.email "填写你的邮箱"[root@lb01 gitroot]# git config --global user.name "用户名"[root@lb01 gitroot]#

然后,重新提交:

[root@lb01 gitroot]# git commit -m "add 1.txt"[master (root-commit) 13bfc36] add 1.txt 1 file changed, 1 insertion(+) create mode 100644 1.txt[root@lb01 gitroot]#

修改1.txt

[root@lb01 gitroot]# echo hahah >> 1.txt [root@lb01 gitroot]# git add 1.txt[root@lb01 gitroot]# git commit -m "ch 1.txt agin"[master 72683d5] ch 1.txt agin 1 file changed, 1 insertion(+)[root@lb01 gitroot]#

查看:

[root@lb01 gitroot]# git log --pretty=oneline72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt agin13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt[root@lb01 gitroot]#

OK,有两个版本。最底下的为最初的版本,回退到最初的版本:

[root@lb01 gitroot]# git reset --hard 13bfc36HEAD is now at 13bfc36 add 1.txt[root@lb01 gitroot]#

查看一下内容:

[root@lb01 gitroot]# cat 1.txt 123

回退到最新的版本:

[root@lb01 gitroot]# git reset --hard 72683d5HEAD is now at 72683d5 ch 1.txt agin[root@lb01 gitroot]# cat 1.txt 123hahah[root@lb01 gitroot]#

OK成功。

查看所有的版本:

[root@lb01 gitroot]# git reflog72683d5 HEAD@{0}: reset: moving to 72683d513bfc36 HEAD@{1}: reset: moving to 13bfc3672683d5 HEAD@{2}: commit: ch 1.txt aginbbf2058 HEAD@{3}: commit: ch 1.txt13bfc36 HEAD@{4}: commit (initial): add 1.txt[root@lb01 gitroot]#

如果删除了1.txt,怎么恢复呢

git checkout -- 文件名

[root@lb01 gitroot]# ls1.txt[root@lb01 gitroot]# rm -f 1.txt [root@lb01 gitroot]# git checkout -- 1.txt[root@lb01 gitroot]# ls1.txt[root@lb01 gitroot]#

如果文件修改,但没有提交。怎么回退到上一次的状态:

[root@lb01 gitroot]# cat 1.txt 123shalddcbhahah[root@lb01 gitroot]# echo *** > 1.txt [root@lb01 gitroot]# git add 1.txt[root@lb01 gitroot]# git reset HEAD 1.txtUnstaged changes after reset:M	1.txt[root@lb01 gitroot]# git checkout -- 1.txt[root@lb01 gitroot]# cat 1.txt 123shalddcbhahah[root@lb01 gitroot]#

删除文件:

[root@lb01 gitroot]# git rm 1.txt rm '1.txt'[root@lb01 gitroot]# git commit -m "rm 1.txt"[master 696db6e] rm 1.txt 1 file changed, 5 deletions(-) delete mode 100644 1.txt[root@lb01 gitroot]#

此时的删除用checkout无法恢复:

[root@lb01 gitroot]# git checkout -- 1.txterror: pathspec '1.txt' did not match any file(s) known to git.[root@lb01 gitroot]#

可以使用reset恢复:

[root@lb01 gitroot]# git log --pretty=oneline696db6e8bc327f14e08c97a317b6aac35c25ab63 rm 1.txt72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt aginbbf2058fa98c7f74c53aa20d1e11bbe12d903c96 ch 1.txt13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt[root@lb01 gitroot]# git reset --hard 72683HEAD is now at 72683d5 ch 1.txt agin[root@lb01 gitroot]# ls1.txt[root@lb01 gitroot]# cat 1.txt 123shalddcbhahah[root@lb01 gitroot]#

六、建立远程仓库

1、到https://github.com/注册一个账号

2、创建一个仓库

551ca13affa0b585911914c03ee889a4925.jpg

点击+号,New repository。

828f4f31c1dd2e83345d984891fa6ba602a.jpg

设置密钥,点击头像的倒三角,选择setting

6fc3bd93e56fa01e41a2b752bc1a808a57b.jpg

f3ec495ddfa7f2d39e56301617baf5b6dfe.jpg

 

在Linux主机上生成ssh公钥:

[root@lb01 ~]# 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:SHA256:Pkty2n/p5BEFYo/VUrU50xkBg/u5u3HQFlfBbwhxTII root@lb01The key's randomart image is:+---[RSA 2048]----+|          o.*O*==||         .E*o++.B||          . oo.O+||           . .o B||        S   o..+ ||       .     +o  ||      . =   oo.. ||       * o oooo  ||      . o..oooo  |+----[SHA256]-----+[root@lb01 ~]#

把公钥的内容添加到:

9ba4a0cf986f86183c46b287fd5bd29d705.jpg

添加成功如下:

1f0fb39b7fe8bd06e1f4f9eee45c103527f.jpg

在Linux上测试:

创建测试目录并初始化

[root@lb01 ~]# mkdir /mygit[root@lb01 ~]# cd /mygit/[root@lb01 mygit]# git initInitialized empty Git repository in /mygit/.git/[root@lb01 mygit]#

创建测试文件,并提交:

[root@lb01 mygit]# echo 123456 > haha.txt[root@lb01 mygit]# git add haha.txt [root@lb01 mygit]# git commit -m "hdjkkr1114" [master (root-commit) f2c2b18] hdjkkr1114 1 file changed, 1 insertion(+) create mode 100644 haha.txt[root@lb01 mygit]# git remote add origin https://github.com/yanyuzm/mytest.git[root@lb01 mygit]# git push -u origin masterUsername for 'https://github.com': 输入用户名Password for 'https://xxxx@github.com': Counting objects: 3, done.Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To https://github.com/yanyuzm/mytest.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.[root@lb01 mygit]#

OK,推送成功:

e3a11e8cf3f1482b9f96095375fa87a97e9.jpg

七、克隆远程仓库

克隆远程仓库命令:git clone git@github.com:用户名/仓库名

[root@lb01 ~]# cd /home/[root@lb01 home]# git clone git@github.com:yanyuzm/mytestCloning into 'mytest'...The authenticity of host 'github.com (13.250.177.223)' 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)? yesWarning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.remote: Counting objects: 6, done.remote: Compressing objects: 100% (3/3), done.remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0Receiving objects: 100% (6/6), done.[root@lb01 home]#

查看一下:

[root@lb01 home]# ls mytest/2.txt  haha.txt

克隆成功。

修改文件并更新到远程仓库:

[root@lb01 ~]# cd /home/mytest/[root@lb01 mytest]# [root@lb01 mytest]# echo "****" > 2.txt [root@lb01 mytest]# cat 2.txt ****[root@lb01 mytest]# git add 2.txt [root@lb01 mytest]# git commit -m "change 2.txt"[master 3864774] change 2.txt 1 file changed, 1 insertion(+), 1 deletion(-)[root@lb01 mytest]# git pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:  git config --global push.default matchingTo squelch this message and adopt the new behavior now, use:  git config --global push.default simpleSee '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)Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.Counting objects: 5, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 262 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:yanyuzm/mytest   cd83eaa..3864774  master -> master[root@lb01 mytest]#

到github查看2.txt文件:

32d728b1b4361ff236d3dcdf6298003fdbb.jpg

OK,更改成功。

在Linux上使用git pull命令,可以把远程仓库的文件更新到本地。

[root@lb01 mytest]# git pullremote: Counting objects: 3, done.remote: Compressing objects: 100% (2/2), done.Unpacking objects: 100% (3/3), done.remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0From github.com:yanyuzm/mytest   3864774..c23c55d  master     -> origin/masterUpdating 3864774..c23c55dFast-forward 2.txt | 1 + 1 file changed, 1 insertion(+)[root@lb01 mytest]#

八、分支管理

1、查看分支

git branch

[root@lb01 mytest]# git branch* master[root@lb01 mytest]#

2、创建分支

[root@lb01 mytest]# git branch test[root@lb01 mytest]# git branch* master  test[root@lb01 mytest]#

*:表示当前所在的分区

3、切换到test分支

[root@lb01 mytest]# git checkout testSwitched to branch 'test'[root@lb01 mytest]# git branch  master* test[root@lb01 mytest]#

4、在test分支中创建并推送文件

[root@lb01 mytest]# echo "test123" > 3.txt[root@lb01 mytest]# git add 3.txt [root@lb01 mytest]# git commit -m "add 3.txt"[test b3e4bb8] add 3.txt 1 file changed, 1 insertion(+) create mode 100644 3.txt[root@lb01 mytest]# ls2.txt  3.txt  haha.txt[root@lb01 mytest]#

切换到master分支查看:

[root@lb01 mytest]# ls2.txt  3.txt  haha.txt[root@lb01 mytest]# git checkout masterSwitched to branch 'master'[root@lb01 mytest]# ls2.txt  haha.txt[root@lb01 mytest]#

合并分支git merge:

[root@lb01 mytest]# git branch* master  test[root@lb01 mytest]# git merge testUpdating c23c55d..b3e4bb8Fast-forward 3.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 3.txt[root@lb01 mytest]# ls2.txt  3.txt  haha.txt[root@lb01 mytest]#

如果当前分支的文件有了修改,再合并:

[root@lb01 mytest]# ls2.txt  3.txt  haha.txt[root@lb01 mytest]# echo "+787" >> 3.txt [root@lb01 mytest]# git add 3.txt [root@lb01 mytest]# git commit -m "ch 3.txt"[master b6ba74d] ch 3.txt 1 file changed, 1 insertion(+)[root@lb01 mytest]# git checkout testSwitched to branch 'test'[root@lb01 mytest]# echo "88888" >> 3.txt [root@lb01 mytest]# git add 3.txt [root@lb01 mytest]# git commit -m "ch 3.txt"[test f386e1f] ch 3.txt 1 file changed, 1 insertion(+)[root@lb01 mytest]#

master、test分支中的3.txt文件内容都不一样,那么怎么合并呢?

[root@lb01 mytest]# git checkout masterSwitched to branch 'master'Your branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)[root@lb01 mytest]# git merge testAuto-merging 3.txtCONFLICT (content): Merge conflict in 3.txtAutomatic merge failed; fix conflicts and then commit the result.[root@lb01 mytest]#

合并失败,因为文件内容不一样。

只有分支一样才能合并。

删除分支:git branch -D 分支名  或git branch -d 分支名

-D:强制删除

[root@lb01 mytest]# git branch -d testDeleted branch test (was f386e1f).[root@lb01 mytest]# git branch* master[root@lb01 mytest]#

九、远程分支管理

1、分支使用的原则

(1)master分支是非常重要的,线上发布代码使用master分支。平时开发代码不要在这个分支上

(2)创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master

(3)开发人员应该在dev的基础上再分支成个人分支,在个人分支里面开发代码,然后合并到dev分支。

比如dev合并bob分支(dev,bob分支事先创建好):

切换到dev分支:git  checkout dev

然后合并:git merge  bob

2、远程分支

本地创建的分支如果不推送到远程,对其他人是不可见的

查看远程分支:git ls-remote origin

[root@lb01 mytest]# git ls-remote origin8801c91a1e7435e255ac297deebd574126c47ad6	HEAD8801c91a1e7435e255ac297deebd574126c47ad6	refs/heads/dev8801c91a1e7435e255ac297deebd574126c47ad6	refs/heads/master[root@lb01 mytest]#

(1)在https://github.com上创建一个dev分支:

20afc769ea3915cfa106069c5a1a52d63d3.jpg

(2)克隆dev分支到/tmp目录

f5c9550513d4a18f61a2564e972692ce107.jpg

克隆命令如下:

[root@lb01 ~]# cd /tmp[root@lb01 tmp]# git clone git@github.com:yanyuzm/mytest.git[root@lb01 tmp]# lsmytest  vmware-root[root@lb01 tmp]# cd mytest/[root@lb01 mytest]#

查看一下分支:

[root@lb01 mytest]# git branch* master[root@lb01 mytest]#

只有master分支,实际上远程里有dev。克隆默认只克隆master分区。

要想把远程的分支克隆下来,需要手动创建与远程分支对应的分支。创建命令:

git checkout -b 本地分支名 origin/远程分支名

本地分支和远程分支名称要一样。

[root@lb01 mytest]# git checkout -b dev origin/devBranch dev set up to track remote branch dev from origin.Switched to a new branch 'dev'[root@lb01 mytest]# [root@lb01 mytest]# git branch* dev  master[root@lb01 mytest]#

此时在dev分支下,创建一个文件做测试,比如:

[root@lb01 mytest]# echo dev >2.txt[root@lb01 mytest]# git add 2.txt [root@lb01 mytest]# git commit -m "add 2.txt"[dev b3a7372] add 2.txt 1 file changed, 1 insertion(+) create mode 100644 2.txt[root@lb01 mytest]#

更新推送:

[root@lb01 mytest]# git pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:  git config --global push.default matchingTo squelch this message and adopt the new behavior now, use:  git config --global push.default simpleSee '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: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 258 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 git@github.com:yanyuzm/mytest.git   8801c91..b3a7372  dev -> dev[root@lb01 mytest]#

因为master没有变动,所以更新的是dev。

如果本地的master、dev都更新了,那么推送到远程会更新哪个?

git push会把所有本地分支的变更一同推送到远程,如果只想推送一个分支,则使用git push origin 分支名。

如果本地的分支比远程的多,比如在本地再创建一个dev2的分支

[root@lb01 mytest]# git branch dev2[root@lb01 mytest]# git branch* dev  dev2  master[root@lb01 mytest]#

在dev2分支创建一个文件

[root@lb01 mytest]# git checkout dev2Switched to branch 'dev2'[root@lb01 mytest]# echo 888 >1.dev2[root@lb01 mytest]# git add 1.dev2 [root@lb01 mytest]# git commit -m "add 1.dev2"[dev2 6f374e1] add 1.dev2 1 file changed, 1 insertion(+) create mode 100644 1.dev2[root@lb01 mytest]#

现在本地有3个分支:master、dev、dev2,而远程只有master、dev分支。此时推送会怎样?

[root@lb01 mytest]# git config --global push.default matching[root@lb01 mytest]# git pushEverything up-to-date[root@lb01 mytest]#

提示说远程的是最新的,不用更新。因为远程没有dev2,所以本地的dev2是不会推送到远程的。

要推送dev2,则:

[root@lb01 mytest]# git push origin dev2Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 260 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 git@github.com:yanyuzm/mytest.git * [new branch]      dev2 -> dev2[root@lb01 mytest]#

OK,推送成功。远程查看一下:

c9651b9476c0fb3458799f940976bb17f77.jpg

OK,远程更新成功。

十、标签管理

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

先切换到master分支,然后给master打标签,比如v1.0标签。

[root@lb01 mytest]# git checkout masterSwitched to branch 'master'[root@lb01 mytest]# git tag v1.0[root@lb01 mytest]#

查看某个标签的信息:

[root@lb01 mytest]# git show v1.0commit 8801c91a1e7435e255ac297deebd574126c47ad6Author: yanyuzm <42643313+yanyuzm@users.noreply.github.com>Date:   Tue Aug 28 19:54:53 2018 +0800    Create README.mddiff --git a/README.md b/README.mdnew file mode 100644index 0000000..7eee837--- /dev/null+++ b/README.md@@ -0,0 +1,2 @@+# mytest+测试项目[root@lb01 mytest]#

查看全部标签:

[root@lb01 mytest]# git tagv1.0[root@lb01 mytest]#

只打了一个v1.0标签。

标签tag是针对commit来打的,所以可以针对历史的commit打标签。

查看commit历史:

[root@lb01 mytest]# git log --pretty=oneline --abbrev-commit8801c91 Create README.md9d4a158 Add files via upload77ee382 Delete haha.txt408b5ef Delete 2.txt7eb41b7 Merge branch 'test'f386e1f ch 3.txtb6ba74d ch 3.txtb3e4bb8 add 3.txtc23c55d Update 2.txt3864774 change 2.txtcd83eaa change 2.txtfa38175 add 2.txtf2c2b18 hdjkkr1114[root@lb01 mytest]#

显示了所有的commit,比如给3864774打标签:

[root@lb01 mytest]# git tag v0.9 3864774[root@lb01 mytest]# git tagv0.9v1.0[root@lb01 mytest]#

可以对标签进行描述

[root@lb01 mytest]# git tag -a v0.8 -m "tag v0.9 ahahha" f386e1f[root@lb01 mytest]# git tagv0.8v0.9v1.0[root@lb01 mytest]#

删除标签:

[root@lb01 mytest]# git tag -d v0.8Deleted tag 'v0.8' (was 434dc17)[root@lb01 mytest]#

推送某个标签:

[root@lb01 mytest]# git push origin v1.0Total 0 (delta 0), reused 0 (delta 0)To git@github.com:yanyuzm/mytest.git * [new tag]         v1.0 -> v1.0[root@lb01 mytest]#

推送所有标签:

[root@lb01 mytest]# git push --tag originTotal 0 (delta 0), reused 0 (delta 0)To git@github.com:yanyuzm/mytest.git * [new tag]         v0.9 -> v0.9[root@lb01 mytest]#

如果本地删除了一个标签,远程也想要删除,则:

删除本地标签:

[root@lb01 mytest]# git tag v0.9 -d

删除远程标签:

[root@lb01 mytest]# git push origin :refs/tags/v0.9To git@github.com:yanyuzm/mytest.git - [deleted]         v0.9[root@lb01 mytest]#

十一、git别名

git  commit这个命令有点长。可以使用别名

[root@lb01 mytest]# git config --global alias.ci commit[root@lb01 mytest]# echo 555>5.txt[root@lb01 mytest]# git add 5.txt [root@lb01 mytest]# git ci -m "add 5.txt"[master fce5053] add 5.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 5.txt[root@lb01 mytest]#

git branch:

[root@lb01 mytest]# git config --global alias.br branch[root@lb01 mytest]# git br  dev  dev2* master[root@lb01 mytest]#

git checkout:

[root@lb01 mytest]# git config --global alias.co checkout[root@lb01 mytest]# git co devSwitched to branch 'dev'[root@lb01 mytest]# git br* dev  dev2  master[root@lb01 mytest]#

查看别名:

[root@lb01 mytest]# git config --list | grep aliasalias.ci=commitalias.br=branchalias.co=checkout[root@lb01 mytest]#

取消别名:

[root@lb01 mytest]# git config --global --unset alias.br[root@lb01 mytest]# git config --global --unset alias.co[root@lb01 mytest]# git config --global --unset alias.ci

配置文件:/root/.gitconfig

[root@lb01 mytest]# cat /root/.gitconfig [user]	email = haha@qq.com	name = xzm[push]	default = matching[alias]	ci = commit	br = branch	co = checkout[root@lb01 mytest]#

设置查询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"

设置如下:

[root@lb01 mytest]# 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"

显示如下:

[root@lb01 mytest]# git lg* b3a7372 -  (HEAD, origin/dev, dev) add 2.txt (89 minutes ago) 
* 8801c91 - (tag: v1.0, origin/master, origin/HEAD) Create README.md (2 hours ago)
* 9d4a158 - Add files via upload (5 days ago)
* 77ee382 - Delete haha.txt (5 days ago)
* 408b5ef - Delete 2.txt (5 days ago)
* 7eb41b7 - Merge branch 'test' (5 days ago)
|\ | * f386e1f - ch 3.txt (5 days ago)
* | b6ba74d - ch 3.txt (5 days ago)
|/ * b3e4bb8 - add 3.txt (5 days ago)
* c23c55d - Update 2.txt (5 days ago)
* 3864774 - change 2.txt (5 days ago)
* cd83eaa - change 2.txt (5 days ago)
* fa38175 - add 2.txt (5 days ago)
* f2c2b18 - hdjkkr1114 (5 days ago)
[root@lb01 mytest]#

可以显示颜色:

1b6ebbd24b0b45145457ce506c0d0e09e31.jpg

这样好看多了。

十二、搭建git服务器

(一)服务端配置:

(1)服务端安装git

创建git服务器过程

1、找一台服务器(A机:192.168.10.101),安装git

[root@lb01 ~]# yum install git

2、创建git用户,并设置shell为/usr/bin/git-shell,git用户不需要远程登录

[root@lb01 ~]# useradd -s /usr/bin/git-shell git

3、在/homt/git目录中创建authorized_keys文件

[root@lb01 ~]# cd /home/git/[root@lb01 git]# mkdir .ssh[root@lb01 git]# touch .ssh/authorized_keys[root@lb01 git]# chmod 600 .ssh/authorized_keys [root@lb01 git]# chown -R git.git .ssh/[root@lb01 git]#

5、将客户端(比如:192.168.10.102)的ssh公钥复制到A机的/home/git/.ssh/authorized_keys文件中即可。

192.168.10.102主机生成密钥:

[root@lb02 ~]# 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:SHA256:nrQpTTR8mZ+guNarl7ylUnmU3Vd07UxGnCnFwkWCOzg root@lb02The key's randomart image is:+---[RSA 2048]----+|            o.=BB||       .   + +.+B||        + *o..o=.||       o Eo=... +||      . So. +  . ||       Bo+.      ||      +oBo.      ||     ...++       ||      .++.       |+----[SHA256]-----+[root@lb02 ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqrBL+xmZMSVbejL63Jbs0y6SZafXagF9LRmNuVFCAHE45YyEpucLQ0m/NB3vxxK6PFNvyLPs6I2hODKE9TBgekUJuIxGHUz4DjASNZ2YXNyov53ntAPPaavfiqkJwbHS3e25tc5h37xyPxbRqdl27wSQ+csalB5vkey44wHi/Fr3jzDIfbDQffNKMdhH4fBXEJJTcgX2CQ+pLIZK9AxCUxvyP1mwUFZvVaU0AMs/vW1BCVka2n7L3pCx09qgOuWSzqprNhV7U+nSVdrmM96vp2HvtK1gA1hlXnhfImfgYLnmaMfbcFExRcweDF3n6UgoRsl1OVAvvUWv9wNe6seLj root@lb02[root@lb02 ~]#

 

将此密钥复制黏贴到192.168.10.101主机的/home/git/.ssh/authorized_keys文件中后,尝试登录一下:

[root@lb02 ~]# ssh git@192.168.10.101The authenticity of host '192.168.10.101 (192.168.10.101)' can't be established.ECDSA key fingerprint is SHA256:vhwhx0zQx3/5MdeOdJv8Q/+6oCO/B72tauu32Dz+RBs.ECDSA key fingerprint is MD5:3f:7c:88:2b:cd:7d:7a:43:31:e3:6e:fb:e5:cc:2c:15.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.10.101' (ECDSA) to the list of known hosts.fatal: Interactive git shell is not enabled.hint: ~/git-shell-commands should exist and have read and execute access.Connection to 192.168.10.101 closed.[root@lb02 ~]#

验证没问题,但是不能远程登录。因为git用户设置了不可以远程登录。

(2)服务端裸仓库配置

1、创建/data/gitroot目录仓库

[root@lb01 ~]# mkdir /data/gitroot -p

2、初始化仓库

[root@lb01 ~]# cd /data/gitroot/[root@lb01 gitroot]# git init --bare sample.gitInitialized empty Git repository in /data/gitroot/sample.git/[root@lb01 gitroot]#

这里创建的是裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器去改工作区,并且服务器上的git仓库通常以.git结尾。

修改裸仓库的属主属组:

[root@lb01 gitroot]# lssample.git[root@lb01 gitroot]# chown -R git.git sample.git/[root@lb01 gitroot]#

(二)客户端配置:

前面中,客户端已经设置了免密登录

1、安装git

[root@lb02 ~]# yum install git -y

2、克隆远程仓库

[root@lb02 ~]# git clone git@192.168.10.101:/data/gitroot/sample.git Cloning into 'sample'...warning: You appear to have cloned an empty repository.[root@lb02 ~]#

3、进入克隆后的目录

[root@lb02 ~]# cd sample/[root@lb02 sample]# ls -a.  ..  .git[root@lb02 sample]#

创建一个测试文件并推送:

[root@lb02 sample]# touch haha>1.sam[root@lb02 sample]# git add 1.sam [root@lb02 sample]# git commit -m "add 1.sam"*** 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@lb02.(none)')[root@lb02 sample]#

报错,解决方法:

[root@lb02 sample]# git config --global user.email "haha@lb02.com"[root@lb02 sample]# git config --global user.name "lb02"

这里只是测试用,所以邮箱,用户名随便。

OK,重新提交:

[root@lb02 sample]# git commit -m "add 1.sam"[master (root-commit) 2c59a6e] add 1.sam 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 1.sam[root@lb02 sample]#

推送:

[root@lb02 sample]# git pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:  git config --global push.default matchingTo squelch this message and adopt the new behavior now, use:  git config --global push.default simpleSee '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)No refs in common and none specified; doing nothing.Perhaps you should specify a branch such as 'master'.fatal: The remote end hung up unexpectedlyerror: failed to push some refs to 'git@192.168.10.101:/data/gitroot/sample.git'[root@lb02 sample]#

报错,解决:

因为裸仓库没有分支,所以要指定分支推送:

[root@lb02 sample]# git push origin masterCounting objects: 3, done.Writing objects: 100% (3/3), 196 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@192.168.10.101:/data/gitroot/sample.git * [new branch]      master -> master[root@lb02 sample]#

OK,成功。

再创建一个文件推送:

[root@lb02 sample]# echo 888 > 2.sam[root@lb02 sample]# git add 2.sam [root@lb02 sample]# git commit -m "add 2.sam"[master 0c65a18] add 2.sam 1 file changed, 1 insertion(+) create mode 100644 2.sam[root@lb02 sample]# git push origin masterCounting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 256 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@192.168.10.101:/data/gitroot/sample.git   2c59a6e..0c65a18  master -> master[root@lb02 sample]#

到/tmp目录中克隆:

[root@lb02 ~]# [root@lb02 ~]# cd /tmp/[root@lb02 tmp]# lsvmware-root[root@lb02 tmp]# [root@lb02 tmp]# git clone git@192.168.10.101:/data/gitroot/sample.gitCloning into 'sample'...remote: Counting objects: 6, done.remote: Compressing objects: 100% (3/3), done.remote: Total 6 (delta 0), reused 0 (delta 0)Receiving objects: 100% (6/6), done.[root@lb02 tmp]#

查看一下:

[root@lb02 tmp]# ls sample/1.sam  2.sam[root@lb02 tmp]#

OK,克隆成功。

修改/tmp克隆的文件:

[root@lb02 tmp]# lssample  vmware-root[root@lb02 tmp]# cd sample/[root@lb02 sample]# echo 111 > 1.sam [root@lb02 sample]# git add 1.sam [root@lb02 sample]# git commit -m "ch 1.sam"[master 1dc47b4] ch 1.sam 1 file changed, 1 insertion(+)[root@lb02 sample]# git push

然后到/root/sample中拉取刚才的修改:

[root@lb02 sample]# git checkout -fYour branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.  (use "git pull" to update your local branch)[root@lb02 sample]# git pullUpdating 0c65a18..e0b77cfFast-forward 1.sam | 1 + 1 file changed, 1 insertion(+)[root@lb02 sample]# ls1.sam  2.sam  haha[root@lb02 sample]# cat 1.sam haha[root@lb02 sample]#

OK,成功

十三、安装gitlab

gitlab官网:https://about.gitlab.com,有可能打不开

安装方法:https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-11.2.1-ce.0.el7.x86_64.rpm

gitlab清华大学镜像站:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

gitlab分社区版(ce)和企业版(ee),官方推荐安装gitlab至少4G内存。

1、创建repo文件:

[root@lb01 ~]# vim /etc/yum.repos.d/gitlab.repo[gitlab-ce]name=gitlab-cebaseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/enable=1gpgcheck=0

2、安装gitlab-ce

[root@lb01 ~]# yum install gitlab-ce -y

3、配置

直接执行gitlab-ctl reconfigure即可。

[root@lb01 ~]# gitlab-ctl reconfigure。。。。Running handlers:Running handlers completeChef Client finished, 428/614 resources updated in 03 minutes 18 secondsgitlab Reconfigured![root@lb01 ~]#

这个过程需要几分钟。

4、相关命令

gitlab-ctl  stop/restart/start/status:停止、重启、启动、查看状态命令。

启动gitlab:

[root@lb01 ~]# gitlab-ctl startok: run: alertmanager: (pid 13381) 0sok: run: gitaly: (pid 13390) 1sok: run: gitlab-monitor: (pid 13402) 0sok: run: gitlab-workhorse: (pid 13405) 0sok: run: logrotate: (pid 13421) 1sok: run: nginx: (pid 13427) 0sok: run: node-exporter: (pid 13432) 1sok: run: postgres-exporter: (pid 13438) 0sok: run: postgresql: (pid 13443) 0sok: run: prometheus: (pid 13451) 1sok: run: redis: (pid 13457) 0sok: run: redis-exporter: (pid 13464) 1sok: run: sidekiq: (pid 13469) 0sok: run: unicorn: (pid 13476) 1s[root@lb01 ~]#

5、浏览器打开192.168.10.101

46a83c87ba8d068931081a4649955e780b8.jpg

需要修改密码,这里密码改为12345678。这个密码是root密码。修改密码后登陆成功:

a80524a696af46956797f82d35462a52f62.jpg

OK,gitlab安装成功。

十四、gitlab使用

gitlab常用命令:https://www.cnyunwei.cc/archives/1204

安装gitlab时会安装nginx,nginx安装在:/var/opt/gitlab/nginx目录中。

[root@lb01 ~]# ls /var/opt/gitlab/nginxclient_body_temp  conf  fastcgi_temp  logs  nginx.pid  proxy_cache  proxy_temp  scgi_temp  uwsgi_temp[root@lb01 ~]#

web服务配置文件为:/var/opt/gitlab/nginx/conf/gitlab-http.conf

1、创建组

53fbcaef52ff1dcc24e80ac2ad95be8d91c.jpg

点击Create  a project。

b893a421f9b68b877741caf61653dfa7d1b.jpg

点击创建即可。

2、创建项目

af440c92e9def1b66ca803ed30fb3aab5f9.jpg

如下所示:

15329f306f10a5fdaf8b7bed80e3d247cb3.jpg

项目名:mytest1,其他的保持默认即可。创建成功如下:

538c872fdbb841134e3ca3a78bca9507990.jpg

3、创建用户:

3406522fc5a631ea555c1669384a481bfbc.jpg

创建用户页面:

8ab8ba7467683a2cd507954baa488659894.jpg

点击创建即可,密码:12345678。如下:

7d1b66c2685038bab58f8ea94efeb36acbc.jpg

退出root用户,使用zhangsan用户登录。

第一次登录要修改密码:

611d11cbbdd773fe860b66247374dcc8e3b.jpg

修改密码后即可登录。

40a5206ce907d27f1cbe770bb2617124196.jpg

十五、gitlab备份与还原

1、gitlab备份命令:gitlab-rake gitlab:backup:create

[root@lb01 ~]# gitlab-rake gitlab:backup:create

备份存放目录:/var/opt/gitlab/backups/

[root@lb01 ~]# ls /var/opt/gitlab/backups/1535475481_2018_08_29_11.2.1_gitlab_backup.tar[root@lb01 ~]#

2、恢复,先停止unicorn、sidekiq服务再恢复。

停止unicorn、sidekiq服务:

[root@lb01 ~]# gitlab-ctl stop unicornok: down: unicorn: 0s, normally up[root@lb01 ~]# gitlab-ctl stop sidekiqok: down: sidekiq: 0s, normally up[root@lb01 ~]#

恢复:

[root@lb01 ~]# ls /var/opt/gitlab/backups/1535475481_2018_08_29_11.2.1_gitlab_backup.tar[root@lb01 ~]# gitlab-rake gitlab:backup:restore BACKUP=1535475481_2018_08_29_11.2.1

再启动gitlab服务即可

附:gitlab配置文件

配置文件:/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

该文件可以修改host,端口等信息。

转载于:https://my.oschina.net/logmm/blog/1920500

你可能感兴趣的文章
dns资源记录类型等
查看>>
cacti-0.8.7g的安装
查看>>
如何做到人均利润超过阿里巴巴?7-eleven的互联网思维
查看>>
Linux系统使用普通命令删除不掉的文件处理方法
查看>>
算法学习之路|人口普查
查看>>
canon iPF 系列保养墨盒清零方法
查看>>
kubernetes1.9离线部署
查看>>
Emulating Neural Synapses through AI
查看>>
Oracle in与exists语句
查看>>
pt-kill
查看>>
Django启停操作脚本
查看>>
cisco之VTP
查看>>
python的安装
查看>>
搭建PXE网络实现远程装机服务
查看>>
centos 5.7 ZEND server php5.3.8升级5.3.10
查看>>
分析Linux的组和用户
查看>>
Android官方开发文档Training系列课程中文版:手势处理之多点触控处理
查看>>
grep过滤用法介绍(二)
查看>>
Appium1.6.4-beta iPhone真机控件获取 app-inspector
查看>>
RHEL 6.5 部署多个tomcat服务
查看>>