前言:CentOS7 服务器上已有旧原生 Git 仓库,因为原生Git的功能有局限,所以想用GitLab来替代它,为了验证迁移是否可行,所以就只将部分项目迁移至新部署 GitLab上,以下就是迁移过程中遇到的诸多问题,记录于此,也便于以后维护时参考。
一、环境说明
-
操作系统:CentOS7
-
旧源码控制工具:Git + SSH
-
新源码控制工具: GitLab
-
迁移场景:同机迁移(原生 Git → GitLab)
二、操作步骤
步骤1:确认并更换 CentOS7 系统源
CentOS7 官方源已下线,需更换阿里云源,否则无法正常安装软件。执行
yum makecache
等命令,验证系统源是否正常。
如果出现报错类似:
Could not retrieve mirrorlist http://mirrorlist\.centos\.org/?release=7\&arch=x86\_64\&repo=os\&infra=stock error was 14: curl\#6 \- \&\#34;Could not resolve host: mirrorlist\.centos\.org; Unknown error\&\#34;,
就需要更换阿里云源。
操作步骤
-
备份原有源:
mv /etc/yum.repos.d/CentOS/Base.repo /etc/yum.repos.d/CentOS/Base.repo.bak -
写入阿里云源:
bash
cat > /etc/yum.repos.d/CentOS-Base.repo <<'EOF'
[base]
name=CentOS-7 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-7 - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/updates/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-7 - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/extras/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
-
清理并生成缓存:
yum clean all; yum makecache -
验证:
yum repolist,输出阿里云仓库列表即成功。 -
如果请求还是失败,那可能是代理的问题,需要配置 yum 代理:
vim /etc/yum.conf,末尾添加proxy=http://代理IP:代理端口(需账号密码则为proxy=http://账号:密码@代理IP:代理端口),source /etc/yum.conf使其生效,如果没效果,那就新开一个ssh终端再测试是否已ok。
步骤2:添加 GitLab 源并安装
操作步骤
先添加 GitLab 源:
bash
# 先清理旧文件
rm -rf /etc/yum.repos.d/gitlab*
# 写入阿里云 GitLab 源
cat > /etc/yum.repos.d/gitlab-ce.repo <<'EOF'
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.aliyun.com/gitlab-ce/yum/el7/
gpgcheck=0
enabled=1
EOF
然后执行:
bash
yum clean all
yum makecache
yum install -y gitlab-ce
这里需要注意,如果你不介意版本的话,添加阿里云的源也可以,阿里云的版本只能到10.x。但是如果想要比较新的版本,则需要换个源,比如清华的源,它支持17.x的版本。
添加清华 GitLab 源(支持最新版):
bash
rm -rf /etc/yum.repos.d/gitlab*
cat > /etc/yum.repos.d/gitlab.repo <<EOF
[gitlab]
name=GitLab CE
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
gpgcheck=0
enabled=1
EOF
如果是先安装了10.x的版本,又想升级到17.x的版本,需要将旧版本卸载,并且彻底清理残留,否则重装失败。
参考如下:
bash
# 停止服务
gitlab-ctl stop
# 卸载
yum remove -y gitlab-ce
# 删光所有残留数据(关键!)
rm -rf /var/opt/gitlab
rm -rf /var/log/gitlab
rm -rf /opt/gitlab
rm -rf /etc/gitlab
# 重装最新版
yum install -y gitlab-ce --nogpgcheck
步骤3:配置 GitLab 核心参数
核心:修改 IP、仓库存储路径、日志配置,避免系统分区撑满和日志无限制增长。
操作步骤
-
查看磁盘分区:
df -h,优先选择剩余空间大的分区(如 /home)存储仓库和日志。 -
编辑配置文件:
vim /etc/gitlab/gitlab.rb。 -
修改核心配置(替换自身 IP 和路径):
bash
# 1. 网页访问地址
external_url 'http://你的IP'
# 2. 代码仓库存储位置(放到1.4T大硬盘 /home)
git_data_dirs({
"default" => {
"path" => "/home/gitlab-data"
}
})
# 3. 备份目录放 /home
gitlab_rails['backup_path'] = "/home/gitlab-backups"
gitlab_rails['backup_keep_time'] = 604800
# 4. 日志保留天数
logging['logrotate_rotate'] = 5
-
配置生效:
gitlab-ctl reconfigure,重启服务:gitlab-ctl restart。 -
验证:
gitlab-ctl status,确认服务重启成功。
步骤4:同机迁移原生 Git 项目至 GitLab
先在 GitLab 新建空项目(生成哈希路径),再复制旧仓库内容,否则 GitLab 无法识别。
操作步骤(以 /test/projA 为例)
-
GitLab 新建空项目:登录后
→ New project → Create blank project,路径填/test/projA,取消Initialize repository with a README,创建后生成哈希目录。
-
查询哈希路径,执行命令:
→
gitlab-rails console→
project = Project.find_by_full_path("test/projA")→
puts project.disk_path,得到类似
@hashed/6b/86/xxxx的路径。 -
拼接完整路径:
自定义仓库路径 + 哈希路径 + .git(示例:/home/gitlab/data/repositories/@hashed/6b/86/xxxx.git)。 -
复制旧仓库内容,执行命令:
→
gitlab-ctl stop gitaly→
rm -rf 拼接的路径/*→
cp -a /gitrepo/test/projA/* 拼接的路径/→
chown -R git:git 拼接的路径→
gitlab-ctl start gitaly。 -
验证:刷新 GitLab 网页,能看到项目代码、分支即迁移成功。
步骤5:本地测试克隆
操作步骤
-
获取克隆地址:GitLab 项目 →
Clone→ 复制 HTTP 地址。 -
本地手动克隆:
git clone http://你的GitLabIP/test/projA.git,正常克隆即迁移无误。 -
IDEA 拉取(无插件):
Git → Clone,粘贴地址,弹出框中输入 GitLab 账号即可。 -
IDEA 拉取(gitlab插件):输入http地址,点击生成 Token,会跳转到生成access token的页面。如果没有,那就直接到gitlab的管理页面手动申请:GitLab → 头像 →
Preferences → Access Tokens,配置名称、有效期,勾选api、read_user、read_repository、write_repository,生成后复制token到idea插件里面,然后点登录。
可能遇到本地克隆 504 超时
可能是git设置了代理的原因,试着取消gitlab主机所在ip的代理,配置如下:
git config --global http.http://gitlab的ip地址.proxy ""。