如果是公司内的项目用git作为版本控制,不想代码放到github上,怎么来管理,简单描述下
情景描述
假设有以下三台机器:
1. 版本服务器:gitserver
2. 开发者1:dev01
3. 开发者2:dev02
项目的第一行代码是由开发者dev01编写的,然后提交到版本服务器gitserver,然后开发者dev02将版本服务器gitserver上的代码下载到本地进行开发。
开发者dev01创建秘钥
记录公钥,放到gitserver信任中
[root@localhost]# cd
[root@localhost]# mkdir .ssh
[root@localhost]# cd .ssh
[root@localhost .ssh]# ssh-keygen -t rsa
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:
7c:4c:06:3a:e3:88:d3:57:59:06:74:af:44:2c:f7:26 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| .+o= |
| ..Bo. |
| + oo+.. |
| o o = =E.o |
| o o o S +o |
| . . . |
| |
| |
| |
+-----------------+
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApOtUPJGeJzVxaR9SBDwuPrqKWzb+v7+LzrvmQKzjzy8ugf8py5fy0UgVUlsbCiGNWgenKiFQckc7NMiqkvVcpOubUMiMbNssGdron3CLktsjqQaTrkucIKXWrqLoOA/Z0LBfBu/pQXGZdX/aytlw3DwgGgWuU7LyosP/2JQYINlolz9HWny/p0uN7GPf+bFWts4FKWEVz9RICojL8B+bffGJJU7S+XV0Uc8PeTtBnRG6utqMueTArPQSB+uZ3/dG/UB9OsZUXQ35da7BP0Bth693QfyP36/uLC61zVqVRp/iXq3/98Z0r7yJwbvLVquMmH06RhyAdOpDZW3UlokJsQ== root@localhost.localdomain
在gitserver上建立一个空项目
#安装git
[root@localhost ~]# yum install git
已加载插件:fastestmirror
设置安装进程
Loading mirror speeds from cached hostfile
* webtatic: uk.repo.webtatic.com
解决依赖关系
--> 执行事务检查
---> Package git.i686 0:1.7.1-4.el6_7.1 will be 安装
--> 处理依赖关系 perl-Git = 1.7.1-4.el6_7.1,它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 rsync,它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 perl(Git),它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 perl(Error),它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 执行事务检查
---> Package perl-Error.noarch 1:0.17015-4.el6 will be 安装
---> Package perl-Git.noarch 0:1.7.1-4.el6_7.1 will be 安装
---> Package rsync.i686 0:3.0.6-12.el6 will be 安装
--> 完成依赖关系计算
依赖关系解决
=========================================================================================================================
软件包 架构 版本 仓库 大小
=========================================================================================================================
正在安装:
git i686 1.7.1-4.el6_7.1 updates 4.5 M
为依赖而安装:
perl-Error noarch 1:0.17015-4.el6 base 29 k
perl-Git noarch 1.7.1-4.el6_7.1 updates 28 k
rsync i686 3.0.6-12.el6 base 329 k
事务概要
=========================================================================================================================
Install 4 Package(s)
总下载量:4.9 M
Installed size: 15 M
确定吗?[y/N]:y
下载软件包:
(1/4): git-1.7.1-4.el6_7.1.i686.rpm | 4.5 MB 00:10
(2/4): perl-Error-0.17015-4.el6.noarch.rpm | 29 kB 00:00
(3/4): perl-Git-1.7.1-4.el6_7.1.noarch.rpm | 28 kB 00:00
(4/4): rsync-3.0.6-12.el6.i686.rpm | 329 kB 00:00
-------------------------------------------------------------------------------------------------------------------------
总计 432 kB/s | 4.9 MB 00:11
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在安装 : 1:perl-Error-0.17015-4.el6.noarch 1/4
正在安装 : rsync-3.0.6-12.el6.i686 2/4
正在安装 : git-1.7.1-4.el6_7.1.i686 3/4
正在安装 : perl-Git-1.7.1-4.el6_7.1.noarch 4/4
Verifying : perl-Git-1.7.1-4.el6_7.1.noarch 1/4
Verifying : 1:perl-Error-0.17015-4.el6.noarch 2/4
Verifying : rsync-3.0.6-12.el6.i686 3/4
Verifying : git-1.7.1-4.el6_7.1.i686 4/4
已安装:
git.i686 0:1.7.1-4.el6_7.1
作为依赖被安装:
perl-Error.noarch 1:0.17015-4.el6 perl-Git.noarch 0:1.7.1-4.el6_7.1 rsync.i686 0:3.0.6-12.el6
完毕!
#创建git用户
[root@localhost backup.git]# useradd git
[root@localhost ~]# su - git
#建立同开发者dev01和dev02的信任
[git@localhost ~]$ cd .ssh/
[git@localhost .ssh]$ ls
known_hosts
[git@localhost .ssh]$ pwd
/home/git/.ssh
[git@localhost .ssh]$ vi authorized_keys
[git@localhost .ssh]$ cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApOtUPJGeJzVxaR9SBDwuPrqKWzb+v7+LzrvmQKzjzy8ugf8py5fy0UgVUlsbCiGNWgenKiFQckc7NMiqkvVcpOubUMiMbNssGdron3CLktsjqQaTrkucIKXWrqLoOA/Z0LBfBu/pQXGZdX/aytlw3DwgGgWuU7LyosP/2JQYINlolz9HWny/p0uN7GPf+bFWts4FKWEVz9RICojL8B+bffGJJU7S+XV0Uc8PeTtBnRG6utqMueTArPQSB+uZ3/dG/UB9OsZUXQ35da7BP0Bth693QfyP36/uLC61zVqVRp/iXq3/98Z0r7yJwbvLVquMmH06RhyAdOpDZW3UlokJsQ== root@localhost.localdomain
#建立git目录
[git@localhost .ssh]$ cd
[git@localhost ~]$ pwd
/home/git
[git@localhost ~]$ mkdir gitdir
[git@localhost ~]$ cd gitdir/
[git@localhost gitdir]$ pwd
/home/git/gitdir
[git@localhost gitdir]$ git init --bare
Initialized empty Git repository in /home/git/gitdir/
[git@localhost gitdir]$ pwd
/home/git/gitdir
[git@localhost gitdir]$ ls
branches config description HEAD hooks info objects refs
[git@localhost gitdir]$ git log
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
开发者dev01上设置
#安装git
[root@localhost ~]# yum install git
已加载插件:fastestmirror
设置安装进程
Loading mirror speeds from cached hostfile
* webtatic: sp.repo.webtatic.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 4.3 MB 00:15
webtatic | 3.6 kB 00:00
解决依赖关系
--> 执行事务检查
---> Package git.i686 0:1.7.1-4.el6_7.1 will be 安装
--> 处理依赖关系 perl-Git = 1.7.1-4.el6_7.1,它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 rsync,它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 perl(Git),它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 处理依赖关系 perl(Error),它被软件包 git-1.7.1-4.el6_7.1.i686 需要
--> 执行事务检查
---> Package perl-Error.noarch 1:0.17015-4.el6 will be 安装
---> Package perl-Git.noarch 0:1.7.1-4.el6_7.1 will be 安装
---> Package rsync.i686 0:3.0.6-12.el6 will be 安装
--> 完成依赖关系计算
依赖关系解决
==========================================================================================================================
软件包 架构 版本 仓库 大小
==========================================================================================================================
正在安装:
git i686 1.7.1-4.el6_7.1 updates 4.5 M
为依赖而安装:
perl-Error noarch 1:0.17015-4.el6 base 29 k
perl-Git noarch 1.7.1-4.el6_7.1 updates 28 k
rsync i686 3.0.6-12.el6 base 329 k
事务概要
==========================================================================================================================
Install 4 Package(s)
总下载量:4.9 M
Installed size: 15 M
确定吗?[y/N]:y
下载软件包:
(1/4): git-1.7.1-4.el6_7.1.i686.rpm | 4.5 MB 00:11
(2/4): perl-Error-0.17015-4.el6.noarch.rpm | 29 kB 00:00
(3/4): perl-Git-1.7.1-4.el6_7.1.noarch.rpm | 28 kB 00:00
(4/4): rsync-3.0.6-12.el6.i686.rpm | 329 kB 00:00
--------------------------------------------------------------------------------------------------------------------------
总计 376 kB/s | 4.9 MB 00:13
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在安装 : 1:perl-Error-0.17015-4.el6.noarch 1/4
#创建目录
[root@localhost ~]# mkdir -p data/source
[root@localhost ~]# cd data/source/
#配置作者信息
[root@localhost source]# git config --global user.name "yang"
[root@localhost source]# git config --global user.email "x695@qq.com"
#在当前目录初始化仓库、空提交
[root@localhost source]# git init
Initialized empty Git repository in /root/data/source/.git/
[root@localhost source]# git commit --allow-empty -am "Init"
[master (root-commit) d33561f] Init
#添加远端仓库
[root@localhost source]# git remote add origin git@gitserver:/home/git/gitdir
#本地开发编码
[root@localhost source]# touch README.md
[root@localhost source]# vi README.md
[root@localhost source]# cat README.md
a
b
#将所有文件加入
[root@localhost source]# git add *
#初次提交
[root@localhost source]# git commit -m "Frist file"
[master 99af509] Frist file
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 README.md
#看日志
[root@localhost source]# git log
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
#看状态
[root@localhost source]# git status
# On branch master
nothing to commit (working directory clean)
#推送到远端仓库(gitserver端)
[root@localhost source]# git push origin master
Counting objects: 4, done.
Writing objects: 100% (3/3), 237 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitserver:/home/git/gitdir
a5d49cb..99af509 master -> master
开发者dev02从gitserver上拉取项目
这个是suse系统。
#这个是suse系统,软件包安装方式和centos和rhel略有不同。
linux-qvvt:~ # zypper install git
Retrieving repository 'openSUSE_13.1_Updates' metadata ............................................................[done]
Building repository 'openSUSE_13.1_Updates' cache .................................................................[done]
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following 3 NEW packages are going to be installed:
git git-core perl-Error
The following 6 packages are recommended, but will not be installed (only required packages will be installed):
git-cvs git-email git-gui git-svn git-web gitk
The following package is suggested, but will not be installed:
git-daemon
3 new packages to install.
Overall download size: 3.2 MiB. After the operation, additional 19.3 MiB will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package perl-Error-0.17020-2.1.2.noarch (1/3), 28.0 KiB ( 49.5 KiB unpacked)
Retrieving: perl-Error-0.17020-2.1.2.noarch.rpm ...................................................................[done]
Retrieving package git-core-1.8.4.5-3.14.1.x86_64 (2/3), 3.1 MiB ( 19.2 MiB unpacked)
Retrieving: git-core-1.8.4.5-3.14.1.x86_64.rpm ......................................................[done (211.2 KiB/s)]
Retrieving package git-1.8.4.5-3.14.1.x86_64 (3/3), 57.8 KiB ( 2.6 KiB unpacked)
Retrieving: git-1.8.4.5-3.14.1.x86_64.rpm ............................................................[done (39.3 KiB/s)]
(1/3) Installing: perl-Error-0.17020-2.1.2 ........................................................................[done]
(2/3) Installing: git-core-1.8.4.5-3.14.1 .........................................................................[done]
(3/3) Installing: git-1.8.4.5-3.14.1 ..............................................................................[done]
#创建项目目录
linux-qvvt:~ # pwd
/root
linux-qvvt:~ # mkdir gittest
linux-qvvt:~ # cd gittest/
linux-qvvt:~/gittest # pwd
/root/gittest
#从gitserver上克隆项目
linux-qvvt:~/gittest # git clone git@192.168.163.129:/home/git/gitdir
Cloning into 'gitdir'...
The authenticity of host '192.168.163.129 (192.168.163.129)' can't be established.
RSA key fingerprint is 14:3a:85:6c:88:7f:6c:36:14:4a:15:1d:9b:da:5f:41.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.163.129' (RSA) to the list of known hosts.
git@192.168.163.129's password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
Checking connectivity... done
linux-qvvt:~/gittest # ls
gitdir
linux-qvvt:~/gittest # cd gitdir/
linux-qvvt:~/gittest/gitdir # ls
README.md
linux-qvvt:~/gittest/gitdir # cat README.md
a
b
linux-qvvt:~/gittest/gitdir #
开发者dev01 进行了更改并提交到版本服务器gitservet
[root@localhost source]# cat README.md
a
b
[root@localhost source]# vi README.md
[root@localhost source]# git commit -am "Second change"
[master c238a5f] Second change
1 files changed, 1 insertions(+), 2 deletions(-)
[root@localhost source]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 245 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitserver:/home/git/gitdir
99af509..c238a5f master -> master
[root@localhost source]# git log
commit c238a5f6567f303031f9e50f4833719b0994d651
Author: yang <x695@qq.com>
Date: Mon Apr 4 20:09:32 2016 +0800
Second change
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
[root@localhost source]#
gitserver上看下日志
[git@localhost gitdir]$ git log
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
[git@localhost gitdir]$ git status
fatal: This operation must be run in a work tree
[git@localhost gitdir]$ git log
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
[git@localhost gitdir]$ git branch
* master
开发者02在进行编码前获取最新gitserver上最新代码
发现已经改变过来了。
linux-qvvt:~/gittest/gitdir # git pull origin master
git@192.168.163.129's password:
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.163.129:/home/git/gitdir
* branch master -> FETCH_HEAD
99af509..c238a5f master -> origin/master
Updating 99af509..c238a5f
Fast-forward
README.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
linux-qvvt:~/gittest/gitdir # git log
commit c238a5f6567f303031f9e50f4833719b0994d651
Author: yang <x695@qq.com>
Date: Mon Apr 4 20:09:32 2016 +0800
Second change
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <x695@qq.com>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
linux-qvvt:~/gittest/gitdir # git status
# On branch master
nothing to commit, working directory clean
linux-qvvt:~/gittest/gitdir # ls
README.md
linux-qvvt:~/gittest/gitdir # cat README.md
Hello Git!
linux-qvvt:~/gittest/gitdir #
小结
上面通过简单的场景演示了,多个开发者本地版本库和gitserver远端仓库的交互,在实际项目中的流程回比这个更复杂,会涉及多个分支,还有分支的合并等等。。。