搭建repo服务器管理多个git工程

搭建repo服务器管理多个git工程

原文链接: http://www.lisongze.com/2018/11/02/repo/ or http://www.lisongze.cn/2018/11/02/repo/

1.repo介绍

android使用git作为代码管理工具,开发了gerrit进行代码审核以便更好的对代码进行集中式管理。还开发了repo命令行工具,对git部分命令进行封装,将百多个git库有效的组织。

鉴于repo能够管理多个git库,针对一个项目需要多个git库分开管理使用repo就非常方便。如嵌入式项目一般由uboot、kernel、文件系统rootfs、用户程序等组成。这里就以这样的项目组织来搭建repo服务器。

├── kernel
│   └── linux-3.5.y
├── rootfs
│   └── rootfs
├── uboot
│   └── uboot-2018.11
└── userdata
    └── UartTest

服务器:192.168.3.5
账户:git

2.下载repo

git-repo下载可在服务器端通过以下任一方式下载。

git clone https://gerrit.googlesource.com/git-repo		(谷歌官方源)
git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo	(国内清华源)
git clone https://gerrit-googlesource.lug.ustc.edu.cn/git-repo	(国内中科大源)

在服务端git用户下

$ mkdir -p tools/git-repo
$ cd tools/git-repo
$ pwd
/home/git/tools/git-repo
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -b stable

3.初始化工程和mainifest git仓库

3.1 初始化项目代码仓库 git server端

git@lisongze-virtual-machine:~$ mkdir exynos4412
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/kernel/linux-3.5.y.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/uboot/uboot-2018.11.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/rootfs/rootfs.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/userdata/UartTest.git

git@lisongze-virtual-machine:~$ cd exynos4412/platform/kernel/linux-3.5.y.git
git@lisongze-virtual-machine:~/exynos4412/platform/kernel/linux-3.5.y.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/kernel/linux-3.5.y.git/
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/uboot/uboot-2018.11.git
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/uboot/uboot-2018.11.git/
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/rootfs/rootfs.git
git@lisongze-virtual-machine:~/exynos4412/platform/rootfs/rootfs.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/rootfs/rootfs.git/
git@lisongze-virtual-machine:~/exynos4412/platform/rootfs/rootfs.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/userdata/UartTest.git
git@lisongze-virtual-machine:~/exynos4412/platform/userdata/UartTest.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/userdata/UartTest.git/
git@lisongze-virtual-machine:~/exynos4412/platform/userdata/UartTest.git$ cd

3.2 初始化manifest仓库 git server端

3.2.1 初始化 manifest git server端

git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/manifest.git
git@lisongze-virtual-machine:~$ cd
git@lisongze-virtual-machine:~/exynos4412/platform/manifest.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/manifest.git/

3.2.2 mainfest 提交default.xml

git@lisongze-virtual-machine:~/exynos4412$ git clone git@192.168.3.5:~/exynos4412/platform/mainfest.git
正克隆到 'mainfest'...
git@192.168.3.5's password:
warning: 您似乎克隆了一个空仓库。

default.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote  name="linux"
           fetch="ssh://192.168.3.5/home/git/exynos4412" />
  <default revision="master"
           remote="linux"
           sync-j="1" />

  <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
  <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
  <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
  <project path="userdata/UartTest" name="platform/userdata/UartTest" />

</manifest>

remote name:远程仓库名
revision:分支名

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git comit -m "add default.xml"
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin master

3.3 客户端git提交初始代码

在客户端 192.168.3.xxx源码工程下提交初始代码推送到远程服务器下对应的git仓库。

//在exynos4412/kernel/linux-3.5.y下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/kernel/linux-3.5.y.git master

//在exynos4412/rootfs/rootfs下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/rootfs/rootfs.git master

//在exynos4412/uboot/uboot-2018.11下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/uboot/uboot-2018.11.git master

//在exynos4412/exynos4412/userdata/UartTest下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/userdata/UartTest.git master

4. repo拉取工程代码

在客户端上repo拉取服务器端代码

repo 需要从git-repo源码中拷贝过来,修改url并加上执行权限chmod 777 repo。

  • 将REPO_URL = ‘https://gerrit.googlesource.com/git-repo’ 修改为 REPO_URL = ‘ssh://192.168.3.5/home/git/tools/git-repo’

如有fatal: branch ‘stable’ has not been signed报错,可将REPO_REV = ‘stable’ 改为 REPO_REV = ‘master’,或着可在git-repo源码中切换到stable分支,git check -b state remote/origin/stable,也可重新获取state分支 git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -b stable

在客户端用户下拉取代码

$ mkdir exynos4412_code
$ cd exynos4412_code
$ ./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git
$ ./repo sync

5. repo创建分支

在项目中我们经常会基于某分支新建分支,来开发新的项目。这里我们演示基于master分支创建tiny4412_evb新分支。

5.1 客户端创建分支并推送远程仓库

./repo forall -pv -c "git checkout remotes/m/master -B tiny4412_evb"
./repo forall -pv -c "git push linux tiny4412_evb"

推送远程仓库

git push [remote-name] [branch-name]

  • remote-name:远程仓库名(一般默认远程分支名称为origin)
  • branch-name: 分支名称
    这里远程分支为linux,是由于在default.xml中定义的。

其中会报两个错误,是由于没有权限导致,如下
error: unpack failed: unable to create temporary object directory
remote: error: cannot lock ref ‘refs/heads/tiny4412_evb’: 不能创建 '/home/git/exynos4412/platform/
解决方法比较暴力,到对应的git服务器端chmod 777 -R object 和 chmod 777 -R refs

5.2 修改远端服务端manifest

git@lisongze-virtual-machine:~/exynos4412/manifest$ git branch -a
* master
  remotes/origin/master
git@lisongze-virtual-machine:~/exynos4412/manifest$ git checkout -b tiny4412_evb remotes/origin/master
分支 'tiny4412_evb' 设置为跟踪来自 'origin' 的远程分支 'master'。
切换到一个新分支 'tiny4412_evb'
git@lisongze-virtual-machine:~/exynos4412/manifest$ vim default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git diff
diff --git a/default.xml b/default.xml
index d1aeaa7..d790a73 100644
--- a/default.xml
+++ b/default.xml
@@ -2,7 +2,7 @@
 <manifest>
   <remote  name="linux"
            fetch="ssh://192.168.3.5/home/git/exynos4412" />
-  <default revision="master"
+  <default revision="tiny4412_evb"
            remote="linux"
            sync-j="1" />

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git commit -m "add new branch tiny4412_evb"
[tiny4412_evb 25d1cf6] add new branch tiny4412_evb
 1 file changed, 1 insertion(+), 1 deletion(-)
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin tiny4412_evb
git@192.168.3.5's password:
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 293 bytes | 293.00 KiB/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.3.5:~/exynos4412/platform/manifest.git
 * [new branch]      tiny4412_evb -> tiny4412_evb

5.3 拉取新建分支代码

mkdir tiny4412_evb
cd tiny4412_evb
./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git -b tiny4412_evb
./repo sync

6. 增加子工程git管理

经常我们需要增加一个单独的子工程需要用git管理,就需要修改远端服务器。

6.1 服务器侧操作—创建目录初始化git server

git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/rootfs/tslib.git
git@lisongze-virtual-machine:~$ cd exynos4412/platform/rootfs/tslib.git
git@lisongze-virtual-machine:~$ git init --bare

6.2 客户端操作-提交代码

这里我示例增加rootfs/tslib源码,在tiny4412_evb分支上。
在tiny4412_evb目录下操作,拷贝tslib源码到rootfs下

./repo forall -pv -c "git checkout -b tiny4412_evb remotes/linux/tiny4412_evb"
cd rootfs/tslib/
git init
git add .
git commit -m "add tslib code"
git branch tiny4412_evb
git checkout tiny4412_evb
git push git@192.168.3.5:/home/git/exynos4412/platform/rootfs/tslib.git tiny4412_evb

6.3 服务器侧操作-修改manifest

git@lisongze-virtual-machine:git checkout -B tiny4412_evb remotes/origin/tiny4412_evb
git@lisongze-virtual-machine:~/exynos4412/manifest$ git branch
  master
* tiny4412_evb
git@lisongze-virtual-machine:~/exynos4412/manifest$ git diff
diff --git a/default.xml b/default.xml
index d790a73..9cb1d41 100644
--- a/default.xml
+++ b/default.xml
@@ -8,6 +8,7 @@

   <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
   <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
+  <project path="rootfs/tslib" name="platform/rootfs/tslib" />
   <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
   <project path="userdata/UartTest" name="platform/userdata/UartTest" />

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git status
位于分支 tiny4412_evb
您的分支与上游分支 'origin/tiny4412_evb' 一致。

要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

        修改:     default.xml

git@lisongze-virtual-machine:~/exynos4412/manifest$ git commit -m "add tslib"
[tiny4412_evb 740d86f] add tslib
 1 file changed, 1 insertion(+)
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin tiny4412_evb
git@192.168.3.5's password:
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 307 bytes | 307.00 KiB/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.3.5:~/exynos4412/platform/manifest.git
   25d1cf6..740d86f  tiny4412_evb -> tiny4412_evb

提交完验证

./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git -b tiny4412_evb
./repo sync

7. 给整个工程打tag标签

git打tag命令:

git tag –a TAG_NAME -m "add tag TAG_NAME"

我们在此版本上打上标签为TINY4412_V1.0.0,命令如下

./repo sync
./repo forall -pv -c "git tag -a TINY4412_V1.0.0 -m "add tag TINY4412_V1.0.0""
./repo forall -pv -c "git push linux TINY4412_V1.0.0"

总结

通过上面方法创建repo服务器,可以看出简单的项目工程还是可以手动创建,但较复杂的工程,如android还是比较麻烦,最好的方式就是通过sh脚本来实现。但原理是相通的,repo 管理一个项目有多个git仓库有强大的优势的。

优化

上述方案中,git push 解决方法比较暴力,到对应的git服务器端chmod 777 -R object 和 chmod 777 -R refs。此次可以使用ssh 公钥来解决。

manifest调整

default.xml
主要修改 fetch=“ssh://git@192.168.3.5/home/git/exynos4412” ,然后git提交上去即可

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote  name="linux"
           fetch="ssh://git@192.168.3.5/home/git/exynos4412" />
  <default revision="master"
           remote="linux"
           sync-j="1" />

  <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
  <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
  <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
  <project path="userdata/UartTest" name="platform/userdata/UartTest" />

</manifest>

拉取代码 带上git@xxx

./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git  -b master

公钥添加方法

在服务端git用户下

$ ssh-keygen
$ cd .ssh
$ touch authorized_keys
$ vim authorized_keys //添加客户端其他用户公钥,保存退出

客户端用户生成公钥

$ ssh-keygen
$ cd .ssh
$ 将id_rsa.pub公钥内容,给管理员,添加到git用户下的.ssh/authorized_keys中即可
$ 验证公钥是否成功
$ ssh -T git@192.168.3.5 提示不用输入密码,即添加公钥成功
  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 搭建一个Git服务器是一个相对容易的过程,以下是一个简单的步骤: 1. 获取一个Linux操作系统,如Ubuntu或CentOS。确保你具有root权限。 2. 在服务器上安装Git软件。使用以下命令在终端中运行: 对于Ubuntu系统: ``` sudo apt-get update sudo apt-get install git ``` 对于CentOS系统: ``` sudo yum update sudo yum install git ``` 3. 创建一个新用户来管理Git服务器。可以使用以下命令创建一个名为"git"的用户: ``` sudo adduser git ``` 设置一个密码并记录下来。 4. 切换到git用户。可以使用以下命令: ``` su git ``` 5. 创建一个专门用于存储Git仓库的目录。可以使用以下命令来创建目录并进入该目录: ``` mkdir gitrepo cd gitrepo ``` 6. 初始化Git仓库。使用以下命令: ``` git init --bare ``` 7. 退出git用户并返回root用户。 ``` exit ``` 8. 配置SSH认证。使用以下命令编辑SSH配置文件: ``` sudo nano /etc/ssh/sshd_config ``` 找到以下行并取消注释修改为"yes",确保SSH密钥认证是启用的: ``` PubkeyAuthentication yes ``` 保存并退出。 9. 重启SSH服务以使更改生效。使用以下命令: 对于Ubuntu系统: ``` sudo service ssh restart ``` 对于CentOS系统: ``` sudo systemctl restart sshd ``` 10. 现在你的Git服务器已经搭建完成。你可以通过用Git客户端克隆服务器上的仓库,例如: ``` git clone git@your_server_ip:/path/to/gitrepo.git ``` 替换"your_server_ip"为你的服务器IP地址,"/path/to/gitrepo.git"为你创建的Git仓库的路径。 这些步骤将让你能够在你的Linux服务器搭建一个Git仓库,并开始使用。请注意,这只是最基本的过程,你还可以通过配置Git的其他特性来增强服务器的功能。 ### 回答2: 搭建一个Git服务器是使用Linux操作系统的常见任务之一。以下是一个简单的步骤指导,帮助你在Linux上搭建一个Git服务器。 1. 首先,在你的Linux服务器上安装Git软件。可以使用系统的包管理器来安装,比如在Ubuntu上可以使用以下命令:`sudo apt-get install git`。 2. 创建一个用于存储Git仓库的目录。可以选择在服务器的任意位置创建,比如 `/opt/git`。使用以下命令创建目录:`sudo mkdir /opt/git`。 3. 通过以下命令进入该目录:`cd /opt/git`。 4. 初始化一个裸仓库(Bare Repository),这是一个没有工作目录的纯粹的Git仓库,用于充当服务器上的中央仓库。使用以下命令创建裸仓库:`sudo git init --bare your_repository_name.git`。这里的`your_repository_name.git`是你想要创建的仓库的名称。 5. 设置仓库的权限。为了安全起见,只有具有权限的用户才能访问和推送到仓库。可以使用以下命令设置权限:`sudo chown -R git:git your_repository_name.git`。这里的`your_repository_name.git`是你刚才创建的仓库的名称。 6. 配置Git服务器,并启用SSH访问。编辑Git服务器的SSH配置文件,可以使用以下命令:`sudo nano /etc/ssh/sshd_config`。在配置文件中添加一行:`AllowUsers git`,然后保存并关闭文件。 7. 重启SSH服务以使配置生效:`sudo service sshd restart`。 8. 现在,可以通过SSH协议克隆或推送到你的Git服务器。例如,使用以下命令克隆仓库:`git clone git@your_server_ip:/opt/git/your_repository_name.git`。请记得将`your_server_ip`替换为你的服务器的IP地址,`your_repository_name.git`替换为你刚才创建的仓库的名称。 通过按照上述步骤,在Linux上搭建一个Git服务器并完成相关配置,你就可以在服务器管理和共享你的Git仓库了。 ### 回答3: 要搭建一个基于Linux操作系统的Git服务器,需要以下步骤: 1. 安装Linux操作系统:在一台服务器上安装Linux系统,可以选择Ubuntu、CentOS等常见的发行版。 2. 安装Git软件:使用Linux系统的包管理器,如apt-get(Ubuntu)或yum(CentOS),安装Git软件。 3. 创建Git用户和组:通过命令行创建一个Git用户和一个属于该用户的组,用于管理Git仓库的访问权限。 4. 安装SSH服务:Git使用SSH协议进行远程仓库的访问和通信。确保安装并启动SSH服务,配置允许Git用户进行SSH访问。 5. 切换到Git用户:通过命令行切换到Git用户,以执行后续的操作。 6. 创建Git仓库:使用Git命令创建一个新的仓库,可以使用"git init"命令来初始化一个空的仓库。 7. 配置Git仓库:通过"git config"命令配置Git仓库的参数,例如仓库名称、用户名、邮箱等。 8. 设置Git远程访问:通过SSH协议设置Git远程访问,提供SSH公钥给用户,以便他们可以通过SSH协议访问Git仓库。 9. 设置Git核心服务器钩子:在Git服务器上配置钩子,可以在提交代码或推送代码等操作时触发一些自定义的操作。 10. 测试Git服务器:确保配置正确且服务器正常运行后,使用Git命令测试访问Git仓库的各种操作,如克隆、提交、推送等。 通过以上步骤,就可以在Linux系统上成功搭建一个Git服务器,为团队提供代码托管和版本控制的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值