Windows Git 多 GitHub 账号切换(GCM 管理凭据,不覆盖原有账号)

场景说明:本地存在两套GitHub账号凭据candyjing1carafeng1926,本次为自动化测试项目单独绑定carafeng1926账号,不删除原有candyjing1凭据,两套账号共存互不干扰

项目本地路径:C:\Users\Cara.Feng\dropxl-automation-testing-sf4980-all-dev

步骤一:GitHub网页前置准备(创建私有仓库 + 生成Classic PAT令牌)

功能目标:在GitHub网页端准备好远程私有仓库,并生成具备私有仓库访问权限的Classic PAT令牌,为后续本地Git推送做准备。

1.1 创建目标私有仓库

  1. 登录GitHub账号carafeng1926,点击右上角【New】新建仓库
  2. Repository name填写:dropxl-automation-testing-all(名称必须完全一致,大小写敏感)
  3. 仓库属性选择:Private(私有仓库)
  4. ❌ 不要勾选:Add a README fileAdd .gitignoreChoose a license,避免本地与远程仓库历史冲突
  5. 点击【Create repository】完成创建。

1.2 生成Classic类型GitHub PAT令牌(私有仓库必备)

  1. GitHub右上角头像 → Settings → 页面拉至底部【Developer settings】
  2. 选择【Personal access tokens】→【Tokens (classic)】
  3. 点击【Generate new token (classic)】新建令牌
  4. 基础配置:
    • Note:备注,例:dropxl automation test local push,用于标识令牌用途
    • Expiration:有效期,推荐90天(不建议选择No expiration,存在安全风险)
  5. 权限勾选【核心】:勾选最上层 ✅ repo(Full control of private repositories) ,勾选后下方所有repo子权限会自动全部选中,仅勾选public_repo无法访问私有仓库
  6. 滚动到页面最下方,点击【Generate token】生成令牌
  7. ⚠️ 重要:立刻复制ghp_开头的令牌字符串保存到记事本,页面刷新后令牌将永久不可查看,丢失需要重新生成。

执行结果

  1. GitHub上存在私有仓库 dropxl-automation-testing-all,仓库为空;
  2. 拿到ghp_开头Classic PAT,拥有私有仓库完整读写权限。

步骤二:查看GCM中已缓存的GitHub账号凭据

功能目标 :查看Git凭据管理器里已经保存了哪些GitHub账号,确认carafeng1926账号是否存在,了解当前已缓存账号列表。

打开PowerShell,进入项目根目录

powershell 复制代码
# 进入项目目录
cd C:\Users\Cara.Feng\dropxl-automation-testing-sf4980-all-dev

# 查看GCM缓存的github账号列表
git credential-manager github list

命令作用git credential-manager github list 列出GCM中所有已缓存授权的GitHub账号。

预期结果 :控制台输出账号列表,存在candyjing1


步骤三:Windows凭据管理器新增GitHub凭证(carafeng1926)

功能目标 :在Windows系统凭据管理器手动添加carafeng1926账号凭证,用于HTTPS方式访问GitHub私有仓库,原有candyjing1凭据保留不删除,实现多账号共存

  1. 在PowerShell执行下面命令,快速打开Windows凭据管理器
powershell 复制代码
control keymgr.dll

命令作用control keymgr.dll 唤起Windows凭据管理器窗口。

  1. 在凭据管理器选择【Windows凭据】→【添加Windows凭据】
  • 网络地址:git:[https://github.com](https://github.com)

  • 用户名:carafeng1926

  • 密码:粘贴上一步生成的GitHub PAT令牌(ghp_开头,Classic token必须勾选完整repo权限)

  1. 点击保存凭证。

执行结果 :凭据管理器新增一条记录git:[https://github.com](https://github.com),存储carafeng1926账号的PAT信息,原有candyjing1凭据保留,不会被覆盖删除。


打开PowerShell,进入项目根目录

powershell 复制代码
# 进入项目目录
cd C:\Users\Cara.Feng\dropxl-automation-testing-sf4980-all-dev

# 查看GCM缓存的github账号列表
git credential-manager github list
# 注销某个账号
git credential-manager github logout 账号名
# 查看当前绑定的远程账号
git credential-manager github list

命令作用git credential-manager github list 列出GCM中所有已缓存授权的GitHub账号。

预期结果 :控制台输出账号列表,包含candyjing1carafeng1926两个账号,代表两个账号凭据都已存在,多账号可共存。

步骤四:修改仓库远程origin地址,URL嵌入账号carafeng1926@(仓库级账号隔离核心)

功能目标 :单独为当前自动化测试项目配置远程地址,URL携带用户名,实现本项目固定使用carafeng1926账号,其他仓库继续使用candyjing1,仓库级别隔离账号。

powershell 复制代码
# 修改origin远程地址,url嵌入carafeng1926@
git remote set-url origin https://carafeng1926@github.com/carafeng1926/dropxl-automation-testing-all.git

# 检查 Git 全局用户名邮箱(确认身份是 carafeng1926)
git config --global user.name
git config --global user.email


# 校验远程地址是否修改成功
git remote -v

# 连通性测试,验证仓库鉴权是否正常
git ls-remote origin

命令作用

  1. git remote set-url origin:修改本地仓库origin远程仓库地址;URL增加carafeng1926@,GCM会自动匹配carafeng1926凭据。
  2. git remote -v:查看远程仓库fetch、push地址,确认地址修改生效。
  3. git ls-remote origin:仅做仓库连通和鉴权校验,不会上传代码,提前验证账号、PAT、仓库地址是否全部正常。

预期结果

  1. git remote -v输出origin地址为:

    origin https://carafeng1926@github.com/carafeng1926/dropxl-automation-testing-all.git (fetch)
    origin https://carafeng1926@github.com/carafeng1926/dropxl-automation-testing-all.git (push)

  2. git ls-remote origin:如弹出账号选择窗口,选择carafeng1926;成功返回一串git commit哈希值,代表鉴权+仓库连通正常。

原理:GCM识别URL中的carafeng1926@,自动选用carafeng1926凭据,不再弹窗询问账号,candyjing1凭据保留不影响其他项目。


步骤五:推送本地代码到远程仓库

功能目标:将本地main分支代码推送到GitHub远程仓库。

powershell 复制代码
# 1. 初始化本地git仓库
git init

# 2. (推荐)添加.gitignore,测试项目可忽略 node_modules、logs、report、env文件等
# 手动新建 .gitignore 或者 echo写入

# 3. 添加所有文件到暂存区
git add .

# 4. 本地提交
git commit -m "feat: initial commit dropxl automation testing"

# 5. 关联远程仓库 origin
git remote add origin https://github.com/carafeng1926/dropxl-automation-testing-sf4960-all-dev-lu.git

# 校验远程地址是否配置正确
git remote -v
# 输出应该是
# origin  https://github.com/carafeng1926/dropxl-automation-testing-all.git (fetch)
# origin  https://github.com/carafeng1926/dropxl-automation-testing-all.git (push)
# 6. 把本地分支重命名为main(新版github默认分支main)
git branch -M main

# 7. 首次推送,-u 设置上游关联
git push -u origin main

# 推送本地main分支代码,-u绑定上游远程分支关联关系
git push -u origin main
  1. git push -u origin main:把本地main分支推送到远程origin仓库;-u建立本地main和远程origin/main的上下游绑定,后续直接执行git push即可推送。

预期结果

  1. 作者配置完成后,后续git commit提交记录作者为carafeng1926;
  2. git push执行成功,代码上传至dropxl-automation-testing-all远程仓库,可在GitHub网页查看上传代码。

相关推荐
Wang's Blog2 小时前
Java框架快速入门: Spring Security+OAuth2之核心角色与授权流程
java·spring·github
dong_junshuai2 小时前
每天一个开源项目#100 OpenCodeReview:2.6万星的低噪声AI审查器
程序员·开源·github
峰向AI2 小时前
还在用 Ableton?这个 3.5K Star 的免费音序器,让你在手机上写微分音音乐
github
SL_staff3 小时前
战略落地的最后一公里:从目标到个人日历的自动化链路实现
java·github·全栈
凌奕4 小时前
OpenCodeReview 架构拆解:AI Code Review 为什么不能只是 Git Diff + LLM?
llm·github·agent
m4Rk_8 小时前
【论文阅读】Agent 记忆机制(70):RecMem——只在记忆反复出现时才调用 LLM 做巩固
论文阅读·人工智能·学习·开源·github
u1301308 小时前
GitHub 热榜项目:日榜(2026-09-14)
人工智能·github
林澈在路上9 小时前
能做DJ的AI写歌软件推荐:MELO音乐对比Suno v6
大数据·人工智能·github·音视频·音频
本末倒置1839 小时前
受够微信偷偷更新,我做了一个 Windows 微信更新屏蔽工具
github