如果是公司內的項目用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 [email protected]
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== [email protected]
在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== [email protected]
#建立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 <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
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 "[email protected]"
#在當前目錄初始化倉庫、空提交
[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 <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
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 [email protected]:/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.
[email protected]'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 <[email protected]>
Date: Mon Apr 4 20:09:32 2016 +0800
Second change
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
Date: Mon Apr 4 19:26:52 2016 +0800
Init
[root@localhost source]#
gitserver上看下日誌
[git@localhost gitdir]$ git log
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
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 <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
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 <[email protected]>
Date: Mon Apr 4 20:09:32 2016 +0800
Second change
commit 99af5090f4775579e34793c6a33b4fc871e6cbf8
Author: yang <[email protected]>
Date: Mon Apr 4 19:32:59 2016 +0800
Frist file
commit a5d49cbd016628cdeb385b6c664fcb87bb4947b2
Author: yang <[email protected]>
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遠端倉庫的交互,在實際項目中的流程回比這個更復雜,會涉及多個分支,還有分支的合併等等。。。