SSH秘钥
-
在 Ubuntu 上生成 SSH 密钥对:
-t 指定加密算法,-C 添加注释(通常填邮箱)
ssh-keygen -t ed25519 -C "你的邮箱@example.com" -f ~/.ssh/id_ed25519
执行命令后,一路按回车即可(**注意**:建议不设置 passphrase,否则每次使用 SSH 仍需输入密码)。这会在 `~/.ssh/` 目录下生成 `id_ed25519`(私钥,绝不能泄露)和 `id_ed25519.pub`(公钥)两个文件。
2. **将公钥添加到你的 Git 服务器**:以 GitHub 为例,登录后进入 **Settings** -> **SSH and GPG keys**,点击 **New SSH key**,将 `~/.ssh/id_ed25519.pub` 文件中的内容完整粘贴进去并保存。
3. **修改远程仓库 URL**:这是最关键的一步。你需要将仓库的远程 URL 从 HTTPS 格式切换为 SSH 格式。
bash
```bash
# 查看当前 URL
git remote -v
# 如果显示是 https://... 开头,则需修改
git remote set-url origin git@gitee.com:wei-yuliu/c-language-learning.git
之后执行 git pull 等操作就会自动使用 SSH 密钥进行认证,无需再输入密码。
直接保存账号密码token
# 默认缓存 15 分钟
git config --global credential.helper cache
# 自定义缓存时间,例如 1 小时 (3600 秒)
git config --global credential.helper 'cache --timeout=3600'
#永久存储:凭证会永久保存到磁盘上的一个明文文件 (~/.git-credentials) 中
git config --global credential.helper store