配置Gogs和Github免密git push

本文描述的是在windows系统上配置免密git pushGogs仓库和Gthub仓库

之前也弄过类似操作,但是由于那次重装系统给干没了

Gogs为本地使用,设计为不联网

Github为在线使用,同时使用的还有Github Pages的博客更新

前提

  1. 本机中已有Github的免密配置,也就是在Github上传了公钥,不变更这部分内容
  2. Github配置的是全局用户,不变更这部分内容,新增的Gogs使用local user进行配置
  3. 尽量保持不改动Github部分的配置

生成用于Gogs的密钥对

在任意目录下启动git bash 窗口(gogs@gogs.net邮箱地址为Gogs上的用户邮箱,不是实际互联网上存在的)

1
2
3
4
5
6
7
8
9
$ ssh-keygen -t ed25519 -C "gogs@gogs.net"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_ed25519): gogs_id_ed25519
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gogs_id_ed25519
Your public key has been saved in gogs_id_ed25519.pub
The key fingerprint is:
……

密钥对拷贝至.ssh目录

  • 由于在创建ssh-key时没有指定保存到.ssh目录,所以手动复制到该目录(C:\Users\xxx\ .ssh)

  • 也可以在稍后创建的config文件中指定密钥对的路径,但是不妥,还是放在统一的目录

创建config文件

  • 资源管理器打开目录C:\Users\xxx\.ssh,创建一个config文件(注意没有后缀)
  • gogs_id_ed25519为前面步骤中创建的密钥对名称
  • id_ed25519为我正在使用的github密钥对名称

注意:gogs使用的是root用户,因为当初没有创建git用户(如果你创建了git用户,则使用git)

1
2
3
4
5
6
7
8
9
10
11
12
13
# gogs
Host 172.31.100.225
HostName 172.31.100.225
PreferredAuthentications publickey
IdentityFile C:/Users/xxx/.ssh/gogs_id_ed25519
User root

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile C:/Users/xxx/.ssh/id_ed25519
User git

上传ssh公钥和测试

  1. 登陆Gogs,头像–用户设置–SSH 密钥–增加密钥

  2. 记事本打开gogs_id_ed25519.pub,复制内容粘贴至密钥内容,填写一个密钥名称,点击增加密钥

  3. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # Gogs测试
    $ ssh -T root@172.31.100.225
    Hi there, You've successfully authenticated, but Gogs does not provide shell access.
    If this is unexpected, please log in with password and setup Gogs under another user.

    # Github测试
    $ ssh -T git@github.com
    Hi kiraster! You've successfully authenticated, but GitHub does not provide shell access.

git配置和初始化(懂的跳过)

  1. 为了演示操作,需要在Gogs创建一个仓库

image-5706

  1. 在本地创建一个目录test

  2. 打开创建的test目录

  3. 右键git bash here打开窗口,执行git初始化命令和配置local user

    1
    2
    3
    4
    5
    6
    7
    git init

    # 使用local配置用户,配置的local user仅对当前本地仓库起作用
    # gogs@gogs.net邮箱地址为Gogs上的用户邮箱,不是实际互联网上存在的

    git config --local user.name "gogs"
    git config --local user.email "gogs@gogs.net"
    1
    2
    3
    4
    5
    6
    7
    $ git init
    Initialized empty Git repository in D:/test/.git/

    $ git config --local user.name "gogs"

    $ git config --local user.email "gogs@gogs.net"

  4. 修改remote仓库地址(由于当时省事没有修改domian)

    1
    git remote add origin root@172.31.100.225:gogs/test.git
  5. 执行剩下命令

    1
    2
    3
    4
    touch README.md
    git add README.md
    git commit -m "first commit"
    git push -u origin master
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    $ touch README.md

    $ git add README.md

    $ git commit -m "first commit"
    [master (root-commit) 6b281e3] first commit
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 README.md

    $ git push -u origin master
    Enumerating objects: 3, done.
    Counting objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 200 bytes | 200.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To 172.31.100.225:gogs/test.git
    * [new branch] master -> master
    Branch 'master' set up to track remote branch 'master' from 'origin'.


  6. Gogs上查看

    image-8002

在Rocky Linux 8.7虚拟机安装Gogs的记录
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×