git与远程仓库配置和使用指南

本文介绍了Git与Github或其他远程仓库的常规配置和使用方法。


配置远程仓库

生成 SSH Key

以Github仓库为例,其他远程仓库配置类似。

由于本地 Git 仓库和 GitHub 仓库之间的传输是通过SSH加密的,所以需要配置验证信息:

使用以下命令生成 SSH Key:

1
2
3
ssh-keygen -t ed25519 -C "your_email@example.com"  # 推荐(Ed25519 算法,更安全)
# 或
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # RSA 4096(兼容性更好)
  • -t:指定密钥类型(ed25519rsa
  • -b:密钥位数(RSA 建议 4096)
  • -C:注释(通常用你的邮箱)

之后会要求确认路径和输入密码,使用默认的一路回车就行。

成功的话会在 ~/ 下生成 .ssh 文件夹,进去,打开 id_rsa.pub,复制里面的全部内容。

回到 github 上,进入 Account => Settings(账户配置)。左边选择 SSH and GPG keys,然后点击 New SSH key 按钮,title 设置标题,粘贴id_rsa.pub里面的全部内容。

为了验证是否成功,输入以下命令:

1
2
3
4
5
6
ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 输入 yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi XXXX! You've successfully authenticated, but GitHub does not provide shell access. # 成功信息

以上命令说明已成功连上 Github。


为不同远程仓库生成不同的 SSH Key

GitHub

1
ssh-keygen -t ed25519 -C "github@example.com" -f ~/.ssh/github_ed25519
  • 文件名~/.ssh/github_ed25519(私钥)和 ~/.ssh/github_ed25519.pub(公钥)
  • 添加公钥:复制 github_ed25519.pub 内容到 GitHub → SettingsSSH and GPG keys

GitLab

1
ssh-keygen -t rsa -b 4096 -C "gitlab@example.com" -f ~/.ssh/gitlab_rsa
  • 文件名~/.ssh/gitlab_rsa(私钥)和 ~/.ssh/gitlab_rsa.pub(公钥)
  • 添加公钥:复制 gitlab_rsa.pub 内容到 GitLab → PreferencesSSH Keys

Gitee(码云)

1
ssh-keygen -t ed25519 -C "gitee@example.com" -f ~/.ssh/gitee_ed25519
  • 文件名~/.ssh/gitee_ed25519(私钥)和 ~/.ssh/gitee_ed25519.pub(公钥)
  • 添加公钥:复制 gitee_ed25519.pub 内容到 Gitee → 设置SSH 公钥

配置 ~/.ssh/config 管理多个 SSH Key

1
vim ~/.ssh/config  # 编辑 SSH 配置文件

添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_ed25519

# GitLab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab_rsa

# Gitee
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/gitee_ed25519

配置~/.ssh/config 文件的作用是告诉 SSH 客户端针对不同的远程仓库使用不同的密钥,避免冲突并自动选择正确的密钥进行认证。

保存后运行:

1
chmod 600 ~/.ssh/config  # 设置权限

测试 SSH 连接

1
2
3
ssh -T git@github.com      # 测试 GitHub
ssh -T git@gitlab.com # 测试 GitLab
ssh -T git@gitee.com # 测试 Gitee

如果看到 "Hi username! You've successfully authenticated." 表示配置成功。


本地仓库与远程仓库连接

有两种方式可以使本地与远端连接。

第一种方式,首先在Github上新建仓库,然后将仓库克隆到本地,再将内容移入本地仓库,然后再同步至远端。

1
2
3
4
5
6
7
git clone git@github.com:your-username/your-projects.git
mv local-file your-projects/
git add *
git commit -m "first commit"

git remote add origin git@github.com:your-username/your-projects.git # 将本地仓库关联到远程仓库
$ git push -u origin master # 将本地master分支推送至远端的master分支

第二种方式,首先在Github上新建仓库,然后在本地创建同名仓库目录,在该目录中添加文件,初始化仓库,然后再同步至远端。

1
2
3
4
5
6
7
8
9
10
mkdir your-projects
cd your-projects
... # 增加文件等操作

git init
git add *
git commit -m "first commit"

git remote add origin git@github.com:your-username/your-projects.git # 将本地仓库关联到远程仓库
$ git push -u origin master # 将本地master分支推送至远端的master分支

更多具体git命令参考博文的总结。


参考博文

  1. Git 远程仓库(Github)
  2. Github 简明教程