将阿里云作为git服务器

将阿里云作为git服务器

前言

本来一开始在研究,如何部署在github上的博客自动发布到aliyun上,这样就不用每次更新需要在aliyun服务器上进行一次pull操作

看了些使用git hook操作的文章,没有看到,很多都是一些步骤,也不说明原因,出了问题也不知道怎么解决。想到另外的方法是直接弄一个定时任务,这样也可以进行更新,不过感觉git hook的实现机制比较好。

不过忽然看到可以仓库直接放在aliyun上,aliyun提供了这样的功能,这也算是第一步吧。

流程

  1. aliyun code上添加本地电脑的公钥,建立安全的加密连接
  2. aliyun code上添加项目
  3. 和常规仓库一样的操作

一、在aliyun code上添加本地电脑的公钥,建立安全的加密连接

  1. 查看本机电脑是否有公钥
    公钥文件一般是~/.ssh/id_rsa.pub文件,同时对应一个~/.ssh/id_rsa的私钥文件

  2. 生成公钥文件
    使用命令ssh-keygen -t rsa -C "[email protected]",其实使用ssh-keygen命令即可,-t参数指定算法,-C只是一个描述而已,官方建议是使用阿里云邮箱

  3. 添加本地电脑的公钥,建立安全的加密连接

登陆网站aliyun code,选择设置->SSH公钥->+增加SSH密钥->粘贴公钥的内容->增加密钥

三、在aliyun code上添加项目

登陆aliyun code,选择项目->新建项目->项目设置,因为我的项目已经放在了gitee,所以选择了其他仓库的链接,这样直接就将项目导入了aliyun code,也可以新建一个项目(仓库),pull下来之后,将项目代码复制到该文件夹中。最后点击创建项目

四、修改git同时提交到多个仓库

原理就是增加远程仓库链接

  1. 打开项目中的./git/config文件,其中的内容如下(我的项目):
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = https://gitee.com/mycroftwong/LoveServer.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

可以看到它实际上就是绑定了一个远程仓库地址

  1. 增加远程仓库地址
    增加后的内容如下:
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = https://gitee.com/mycroftwong/LoveServer.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://code.aliyun.com/mycroftwong/LoveServer.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
[remote "aliyun"]
    url = https://code.aliyun.com/mycroftwong/LoveServer.git
    fetch = +refs/heads/*:refs/remotes/gitee/*
    tagopt = --no-tags
  1. 修改文件,进行push

如我修改了README.md文件

# 添加文件
git add README.md
# 提交文件
git commit -m "修改README.md"
# 切换到本地master
git checkout master
# 将本地dev merge到master
git merge dev
# 将本地master push 到remote dev
git push origin master:dev


# 结果如下
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 322 bytes | 322.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Checking connectivity: 3, done.
remote: Powered By Gitee.com
To https://gitee.com/mycroftwong/LoveServer.git
   a8fe678..03d1b99  master -> dev
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 322 bytes | 322.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://code.aliyun.com/mycroftwong/LoveServer.git
   a8fe678..03d1b99  master -> dev

这样就自动提交到了多个仓库

参考文章

使用阿里云code和git管理项目

git同时提交到2个仓库gitee github

git 实现关联 aliyun code 仓库

使用阿里云作为git远程仓库的实践

4.3 服务器上的 Git - 生成 SSH 公钥


   转载规则


《将阿里云作为git服务器》 Mycroft Wong 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
hexo-bug解决方案 hexo-bug解决方案
1. git clone下来的主题,没有添加到项目中前提:一开始知道需要删除.git文件夹 问题:提交之后发现,远程仓库只有一个主题目录,其中的内容并没有提交,尝试进入主题目录,添加所有的文件 # 进入文件夹 $ cd themes/hex
2019-08-12
下一篇 
Spring bug解决方案 Spring bug解决方案
这是在实际过程中遇到的bug及解决方案1. QQ邮件发送错误在本地测试发送邮件没有问题,打包发送到服务器之后,无法发送邮件 问题:端口修改成465,出现错误spring boot Could not connect to SMTP host
  目录