文档章节

代码管理平台

人在艹木中
 人在艹木中
发布于 2018/07/31 22:33
字数 6591
阅读 56
收藏 0

一、代码管理平台介绍

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

版本管理工具发展史: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 = none
auth-access =  write
password-db = passwd
authz-db = authz
realm = /data/svnroot/myproject

4、启动svn

[root@node1 ~]# svnserve -d -r /data/svnroot/
[root@node1 ~]# ps aux | grep svn
root      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/myproject
Authentication realm: <svn://192.168.10.205:3690> /data/svnroot/myproject
Password for 'root': 
Authentication realm: <svn://192.168.10.205:3690> /data/svnroot/myproject
Username: user1
Password for 'user1': 

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

   <svn://192.168.10.205: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@node2 ~]# cd myproject
[root@node2 myproject]# ls -la
total 4
drwxr-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=user1
Checked out revision 0.
[root@node2 ~]# 

进入项目:

[root@node2 ~]# cd myproject/
[root@node2 myproject]# ls
[root@node2 myproject]# ls -la
total 4
drwxr-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         fstab
Transmitting file data .
Committed revision 1.
[root@lb02 myproject]# 

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

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

删除文件:

本地删除:

[root@lb02 myproject]# svn delete haha
D         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 检出

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

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

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

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

登录Linux:

[root@lb02 myproject]# svn up
Updating '.':
A    123.txt
Updated to revision 4.
[root@lb02 myproject]# ls
123.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 init
Initialized 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=oneline
72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt agin
13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt
[root@lb01 gitroot]# 

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

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

查看一下内容:

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

回退到最新的版本:

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

OK成功。

查看所有的版本:

[root@lb01 gitroot]# git reflog
72683d5 HEAD@{0}: reset: moving to 72683d5
13bfc36 HEAD@{1}: reset: moving to 13bfc36
72683d5 HEAD@{2}: commit: ch 1.txt agin
bbf2058 HEAD@{3}: commit: ch 1.txt
13bfc36 HEAD@{4}: commit (initial): add 1.txt
[root@lb01 gitroot]# 

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

git checkout -- 文件名

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

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

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

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

hahah
[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.txt
error: pathspec '1.txt' did not match any file(s) known to git.
[root@lb01 gitroot]# 

可以使用reset恢复:

[root@lb01 gitroot]# git log --pretty=oneline
696db6e8bc327f14e08c97a317b6aac35c25ab63 rm 1.txt
72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt agin
bbf2058fa98c7f74c53aa20d1e11bbe12d903c96 ch 1.txt
13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt
[root@lb01 gitroot]# git reset --hard 72683
HEAD is now at 72683d5 ch 1.txt agin
[root@lb01 gitroot]# ls
1.txt
[root@lb01 gitroot]# cat 1.txt 
123
shald
dcb

hahah
[root@lb01 gitroot]# 

六、建立远程仓库

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

2、创建一个仓库

点击+号,New repository。

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

 

在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@lb01
The 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 ~]# 

把公钥的内容添加到:

添加成功如下:

在Linux上测试:

创建测试目录并初始化

[root@lb01 ~]# mkdir /mygit
[root@lb01 ~]# cd /mygit/
[root@lb01 mygit]# git init
Initialized 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 master
Username 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 -> master
Branch master set up to track remote branch master from origin.
[root@lb01 mygit]# 

OK,推送成功:

七、克隆远程仓库

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

[root@lb01 ~]# cd /home/
[root@lb01 home]# git clone git@github.com:yanyuzm/mytest
Cloning 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)? yes
Warning: 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 0
Receiving 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 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)

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文件:

OK,更改成功。

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

[root@lb01 mytest]# git pull
remote: 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 0
From github.com:yanyuzm/mytest
   3864774..c23c55d  master     -> origin/master
Updating 3864774..c23c55d
Fast-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 test
Switched 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]# ls
2.txt  3.txt  haha.txt
[root@lb01 mytest]# 

切换到master分支查看:

[root@lb01 mytest]# ls
2.txt  3.txt  haha.txt
[root@lb01 mytest]# git checkout master
Switched to branch 'master'
[root@lb01 mytest]# ls
2.txt  haha.txt
[root@lb01 mytest]# 

合并分支git merge:

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

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

[root@lb01 mytest]# ls
2.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 test
Switched 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 master
Switched 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 test
Auto-merging 3.txt
CONFLICT (content): Merge conflict in 3.txt
Automatic 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 test
Deleted 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 origin
8801c91a1e7435e255ac297deebd574126c47ad6	HEAD
8801c91a1e7435e255ac297deebd574126c47ad6	refs/heads/dev
8801c91a1e7435e255ac297deebd574126c47ad6	refs/heads/master
[root@lb01 mytest]# 

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

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

克隆命令如下:

[root@lb01 ~]# cd /tmp
[root@lb01 tmp]# git clone git@github.com:yanyuzm/mytest.git
[root@lb01 tmp]# ls
mytest  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/dev
Branch 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 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: 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 dev2
Switched 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 push
Everything up-to-date
[root@lb01 mytest]# 

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

要推送dev2,则:

[root@lb01 mytest]# git push origin dev2
Counting 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,推送成功。远程查看一下:

OK,远程更新成功。

十、标签管理

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

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

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

查看某个标签的信息:

[root@lb01 mytest]# git show v1.0
commit 8801c91a1e7435e255ac297deebd574126c47ad6
Author: yanyuzm <42643313+yanyuzm@users.noreply.github.com>
Date:   Tue Aug 28 19:54:53 2018 +0800

    Create README.md

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7eee837
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+# mytest
+测试项目
[root@lb01 mytest]#

查看全部标签:

[root@lb01 mytest]# git tag
v1.0
[root@lb01 mytest]#

只打了一个v1.0标签。

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

查看commit历史:

[root@lb01 mytest]# git log --pretty=oneline --abbrev-commit
8801c91 Create README.md
9d4a158 Add files via upload
77ee382 Delete haha.txt
408b5ef Delete 2.txt
7eb41b7 Merge branch 'test'
f386e1f ch 3.txt
b6ba74d ch 3.txt
b3e4bb8 add 3.txt
c23c55d Update 2.txt
3864774 change 2.txt
cd83eaa change 2.txt
fa38175 add 2.txt
f2c2b18 hdjkkr1114
[root@lb01 mytest]# 

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

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

可以对标签进行描述

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

删除标签:

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

推送某个标签:

[root@lb01 mytest]# git push origin v1.0
Total 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 origin
Total 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.9
To 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 dev
Switched to branch 'dev'
[root@lb01 mytest]# git br
* dev
  dev2
  master
[root@lb01 mytest]# 

查看别名:

[root@lb01 mytest]# git config --list | grep alias
alias.ci=commit
alias.br=branch
alias.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) <xzm>
* 8801c91 -  (tag: v1.0, origin/master, origin/HEAD) Create README.md (2 hours ago) <yanyuzm>
* 9d4a158 -  Add files via upload (5 days ago) <yanyuzm>
* 77ee382 -  Delete haha.txt (5 days ago) <yanyuzm>
* 408b5ef -  Delete 2.txt (5 days ago) <yanyuzm>
*   7eb41b7 -  Merge branch 'test' (5 days ago) <xzm>
|\  
| * f386e1f -  ch 3.txt (5 days ago) <xzm>
* | b6ba74d -  ch 3.txt (5 days ago) <xzm>
|/  
* b3e4bb8 -  add 3.txt (5 days ago) <xzm>
* c23c55d -  Update 2.txt (5 days ago) <yanyuzm>
* 3864774 -  change 2.txt (5 days ago) <xzm>
* cd83eaa -  change 2.txt (5 days ago) <xzm>
* fa38175 -  add 2.txt (5 days ago) <xzm>
* f2c2b18 -  hdjkkr1114 (5 days ago) <xzm>
[root@lb01 mytest]# 

可以显示颜色:

这样好看多了。

十二、搭建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@lb02
The 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.101
The 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)? yes
Warning: 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.git
Initialized empty Git repository in /data/gitroot/sample.git/
[root@lb01 gitroot]# 

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

修改裸仓库的属主属组:

[root@lb01 gitroot]# ls
sample.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 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)

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@192.168.10.101:/data/gitroot/sample.git'
[root@lb02 sample]# 

报错,解决:

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

[root@lb02 sample]# git push origin master
Counting 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 master
Counting 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]# ls
vmware-root
[root@lb02 tmp]# 
[root@lb02 tmp]# git clone git@192.168.10.101:/data/gitroot/sample.git
Cloning 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]# ls
sample  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 -f
Your 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 pull
Updating 0c65a18..e0b77cf
Fast-forward
 1.sam | 1 +
 1 file changed, 1 insertion(+)
[root@lb02 sample]# ls
1.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-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
enable=1
gpgcheck=0

2、安装gitlab-ce

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

3、配置

直接执行gitlab-ctl reconfigure即可。

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

这个过程需要几分钟。

4、相关命令

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

启动gitlab:

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

5、浏览器打开192.168.10.101

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

OK,gitlab安装成功。

十四、gitlab使用

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

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

[root@lb01 ~]# ls /var/opt/gitlab/nginx
client_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、创建组

点击Create  a project。

点击创建即可。

2、创建项目

如下所示:

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

3、创建用户:

创建用户页面:

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

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

第一次登录要修改密码:

修改密码后即可登录。

十五、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 unicorn
ok: down: unicorn: 0s, normally up
[root@lb01 ~]# gitlab-ctl stop sidekiq
ok: 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,端口等信息。

© 著作权归作者所有

共有 人打赏支持
上一篇: Docker基础
下一篇: shell编程3
人在艹木中
粉丝 7
博文 59
码字总数 212112
作品 0
茂名
运维
私信 提问
思途旅游 CMS V6.0.201707.21

更新包 产品名 描述 kb2017072006 思途CMS_基础 1、修改代码对https的支持; 优化 kb2017072005 思途CMS_基础 1、帮助按钮功能添加; 优化 kb2017072004 思途CMS_基础 1、1.限制扩展模块拼音...

谢二公子
2017/07/21
552
0
介绍几个常见的Git代码托管平台

一、GitHub 目前最流行的Git服务,也是人气最旺盛的Git代码托管网站,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的Git仓库,私有项目需要付费,个人认为GitHub更适合开源项目使用...

学习也休闲
2016/03/03
364
0
手拉手业务基础平台 V1.4.0 发布

本次版本主要升级durid至1.0.9,在业务基础平台中新增监控管理:会话管理、数据源管理、URL管理、SQL管理等功能,对底层代码实行监控管理,便于优化代码,提升性能。其次修复 参数标签重复性...

平凡哥
2014/10/24
2.1K
1
bboss平台部门管理员配置指南

bboss平台部门管理员配置指南,本文介绍在bboss平台中利用部门管理员实现分级授权功能。 第一部分 为部门分配管理员以及为部门管理员设置权限 1.超级管理员登录系统 admin/123456 2.进入组织管...

bboss
2018/01/21
18
0
J-Hi Java 快速开发平台正式发布

J-HI是什么 J-HI是一款JAVA WEB应用软件快速开发开源平台,主要服务于软件企业和传统行业企事业单位信息中心的开发人员,为他们提供一套完整的一站式的JAVA WEB应用软件快速开发解决方案。 ...

mangogod
2011/02/24
13.7K
10

没有更多内容

加载失败,请刷新页面

加载更多

RabbitMQ入门

RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue)协议的开源实现。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面都非常的优秀。是当前最主流的消息中间...

watermelon11
今天
15
0
今天的学习

自动加载:方法一 function __autoload( $className ){在这里,完成加载B这个类文件的工作。}class A{} //这是一个类$a1 = new A(); //这里没有自动加载的发生,因为A这个类...

墨冥
今天
2
0
印刷工艺步骤

印刷厂从收到订单到交付整个流程,一般涉及到以下步骤 1.设计(经过软件如cdr,psd,ai等等设计需要印刷的名片,宣传单,画册等物料); 2.排版拼版(在电脑软件这区域完成); 3.出版、出硫...

focusone
昨天
4
0
virtualbox中安装ubuntu

virtualbox+ubuntu 安装virtualbox,当前版本是6.0.4 下载ubuntu安装盘,建议lubuntu,链接是http://mirrors.ustc.edu.cn/ubuntu-cdimage/lubuntu/releases/18.04.2/release/lubuntu-18.04.......

chuqq
昨天
5
0
exists 谓词的子查询

https://blog.csdn.net/qq_19782019/article/details/78730882

仟昭
昨天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部