> 技术文档 > 更改git账号_git 修改账号

更改git账号_git 修改账号


更换Git账户

设置Git全局变量

  • 将git.exe和ssh-keygen.exe所在目录加入到环境变量中(ssh-keygen.exe一般在git文件夹的/usr/bin中)

使用SSH方式授权

1. 修改 Git 全局/本地配置

更新用户名和邮箱

  • 全局修改(所有仓库生效)
git config --global user.name \"你的新Gitee用户名\"git config --global user.email \"你的新Gitee邮箱\"
  • 仅修改当前仓库(可选)
git config user.name \"你的新Gitee用户名\"git config user.email \"你的新Gitee邮箱\"

✅ 验证是否修改成功:

git config --get user.namegit config --get user.email

2. 清理旧的 Git 凭据(关键步骤)

由于 Git 会缓存旧账号的凭据,必须清除旧的认证信息,否则仍可能使用旧账号连接。

  1. Windows 系统
  • 打开 控制面板 → 凭据管理器 → Windows 凭据。
  • 在 普通凭据 里找到 git:https://gitee.com 相关的条目,删除。
  1. Mac/Linux 系统
  • 清除 Git 凭据缓存
    git credential-manager reject https://gitee.com
  • 或手动删除
    git config --global --unset credential.helper

3. 生成 SSH 密钥(如果已有可跳过)

推荐使用 ed25519 算法(更安全)

ssh-keygen -t ed25519 -C \"你的Gitee邮箱\"

或者传统 RSA 算法

ssh-keygen -t rsa -b 4096 -C \"你的Gitee邮箱\"

按提示操作:
直接按回车,密钥默认保存在 ~/.ssh/id_ed25519(或 id_rsa)。
可设置密码(可选,增加安全性)。

4. 将公钥添加到 Gitee

  1. 复制公钥内容:
    查看并复制公钥(以 ed25519 为例)
    cat ~/.ssh/id_ed25519.pub
    公钥内容类似:
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJx4… 你的Gitee邮箱
  2. 添加到 Gitee:打开 Gitee → 设置 → SSH 公钥。
    粘贴公钥,标题可自定义(如 My PC),点击 确定。

5. 修改远程仓库 URL 为 SSH 格式

  • 查看当前远程地址
git remote -v
  • 修改为 SSH 地址(SSH地址查看gitee中项目的SSH地址)
git remote set-url origin git@gitee.com:用户名/仓库名.git
  • 再次验证
git remote -v

正确输出:
origin git@gitee.com:用户名/仓库名.git (fetch)
origin git@gitee.com:用户名/仓库名.git (push)

6.测试 SSH 连接

ssh -T git@gitee.com
成功提示:
Hello 你的用户名! You’ve successfully authenticated.