Ubuntu Server 连接 Git 和 GitHub Repo

1. 安装Helix编辑器

使用root用户登录到服务器,安装Helix编辑器。

sudo add-apt-repository ppa:maveonair/helix-editor
sudo apt update
sudo apt install helix

2. 切换到项目ssh用户

sudo su heybran-invoice-builder

3. Generate a New SSH Key (Deploy Key)

Generate a new SSH key specifically for this repository. Run:

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/invoice-builder-repo-key
  • The -f flag specifies the file name to avoid overwriting existing SSH keys.
  • Leave the passphrase blank (recommended for deploy keys).

This command creates two files: invoice-builder-repo-key (private key) and invoice-builder-repo-key.pub (public key) in ~/.ssh/.

Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/heybran-invoice-builder/.ssh/invoice-builder-repo-key
Your public key has been saved in /home/heybran-invoice-builder/.ssh/invoice-builder-repo-key.pub

4. Add the Key to the SSH Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/invoice-builder-repo-key

5. Add the Deploy Key to the GitHub Repository

  • Copy the public key cat /home/heybran-invoice-builder/.ssh/invoice-builder-repo-key.pub
  • Go to your GitHub repository in your web browser.
  • Navigate to Settings > Deploy keys.
  • Click Add deploy key.
  • Paste the public key and give it a descriptive name (e.g., “Ubuntu Server Deploy Key”).
  • Check the Allow write access box if you need push access, and then click Add key.

6. Configure Git to Use the Deploy Key for This Repository

Open or create the ~/.ssh/config file to specify which key to use for this repository, if this file doesn’t exit already.

cd /home/heybran-invoice-builder/.ssh
hx config

Add the following configuration to ~/.ssh/config:

Host adonisjs-invoice-app
    HostName github.com
    User git
    IdentityFile ~/.ssh/invoice-builder-repo

In your local Git repository for AdonisJS-Invoice-App, set the remote to use adonisjs-invoice-app as the host:

git remote set-url origin adonisjs-invoice-app:heybran/AdonisJS-Invoice-App.git

Run this command to test if Git is connecting through the specified SSH config:

ssh -T adonisjs-invoice-app
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi heybran/AdonisJS-Invoice-App! You've successfully authenticated, but GitHub