自己搭建Git服务器实现SSH以及远程仓库迁移

创建git远程仓库:

请参考大神的搭建Git远程创库

迁移远程仓库:

在工作过程由于原来的线上服务器不能用了,需要将上面git管理的原代代码迁移的新的服务器。
我想到了两种方法:
1. 直接从远程仓库clone :

    git clone --bare user@url
    git clone --bare git@127.0.0.1:/srv/code/ams.git

2.先创建一个git空仓库, 再将本地代码push到远程仓库中:

    //创建远程仓库
    git init --bare temp.git
    //修改本地远程仓库地址
    git remote set-url origin git@127.0.0.1:/srv/code/ams.git 

在push到新的远程仓库时可能会遇到下面的错误:

remote: error: insufficient permission for adding an object to repository database ./objects
remote: fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To git@xxx.xxx.xxx.xxx:/srv/resource/ams.git
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'git@xxx.xxx.xxx.xxx:/srv/resource/ams.git'

这是由于在clone或新建远程仓库时用户不是git,用 ls -l 命令查看仓库的拥有者。
如果不是git 可用以下命令修改:

    chown -R [用户]:[用户组] [目录]
    chown -R git:git xxx.git

其中 参数 -R 表示其子目录和文件一同修改。

生成ssh-key

为了安全访问远程仓库一般使用ssh。
1.配置用户名和邮箱:

    //user
    git config --global user.name [username]
    git config --global user.email [email]

2.生成ssh-key:

   git ssh-keygen

window 下生成的key一般在C:\Users\当前用户\ .ssh 这个文件夹下。详细的看这里
这里写图片描述
3. 在linux服务器上添加公钥:
将id_rsa.pub文件打开将里面的字符串复制到服务其上 /home/git/.ssh/authorized_keys 这个文件里面然后保存退出。

    vim /home/git/.ssh/authorized_keys

如果没有这个文件,就用vim新建一个同名文件。添加好后就可以实现ssh访问了


版权声明:本文为q511545671原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。