1 GitSetup
shaotao edited this page 2026-06-12 08:34:24 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Git 配置指南

为什么 push 这么麻烦?怎么一劳永逸?

问题

在服务器上开发时,经常遇到 git push 报认证错误:

remote: Credentials are incorrect or have expired.
fatal: Authentication failed for 'https://git.bx.wedata.club/...'

原因是 git 的 credential store 中存的密码过期了。

方案选择

方案 AHTTPS + Token推荐

为什么用 Token 而不是密码?

  • Token 可以设置独立 scope比密码更安全
  • Token 可以随时吊销,不影响账户密码
  • Forgejo Web UI 支持在 Settings → Applications 生成

配置步骤:

# 1. 把 remote 设为 HTTPS如果之前改过 SSH
git remote set-url origin https://git.bx.wedata.club/bixiu/bixiweave.git

# 2. 将 token 写入 git credential store
echo "https://<用户名>:<token>@git.bx.wedata.club" > ~/.git-credentials

# 3. 确保 credential helper 启用
git config --global credential.helper store

之后所有 git push 自动走 token无需交互。

为什么不直接用 SSH

方式 优点 缺点
HTTPS + Token 配置简单,一次搞定
SSH 无需输密码 需要有服务端 SSH 端口访问权限。Forgejo 的 SSH git 端口通常和服务器登录端口共用(如本机端口 2222意味着持有 SSH key 的人可以直接登录服务器,安全风险较高

方案 BSSH如果能接受安全风险

# 1. 确保 SSH key 已添加到 Forgejo 账户
#    Web UI → Settings → SSH/GPG Keys → Add Key

# 2. 配置 ~/.ssh/config
Host git.bx.wedata.club
    HostName git.bx.wedata.club
    Port 2222
    User git
    IdentityFile ~/.ssh/orion_key
    StrictHostKeyChecking no

# 3. 改 remote 为 SSH 地址
git remote set-url origin git@git.bx.wedata.club:bixiu/bixiweave.git

排查指南

认证失败

# 检查 credential store 内容
cat ~/.git-credentials

# 检查 remote 地址
git remote -v

# 手动测试 token 是否有效(替换具体的 token
curl -H "Authorization: token <token>" https://git.bx.wedata.club/api/v1/user

Token 报 "access token does not exist"

可能原因:

  • Token 已被吊销 → 在 Web UI 重新生成
  • URL 拼写错误 → 检查 ~/.git-credentials 中 URL 是否完全匹配