场景说明:本地存在两套GitHub账号凭据
candyjing1与carafeng1926,本次为自动化测试项目单独绑定carafeng1926账号,不删除原有candyjing1凭据,两套账号共存互不干扰 。项目本地路径:
C:\Users\Cara.Feng\dropxl-automation-testing-sf4980-all-dev
步骤一:GitHub网页前置准备(创建私有仓库 + 生成Classic PAT令牌)
功能目标:在GitHub网页端准备好远程私有仓库,并生成具备私有仓库访问权限的Classic PAT令牌,为后续本地Git推送做准备。
1.1 创建目标私有仓库
- 登录GitHub账号
carafeng1926,点击右上角【New】新建仓库 - Repository name填写:
dropxl-automation-testing-all(名称必须完全一致,大小写敏感) - 仓库属性选择:Private(私有仓库)
- ❌ 不要勾选:
Add a README file、Add .gitignore、Choose a license,避免本地与远程仓库历史冲突 - 点击【Create repository】完成创建。
1.2 生成Classic类型GitHub PAT令牌(私有仓库必备)
- GitHub右上角头像 → Settings → 页面拉至底部【Developer settings】
- 选择【Personal access tokens】→【Tokens (classic)】
- 点击【Generate new token (classic)】新建令牌
- 基础配置:
- Note:备注,例:
dropxl automation test local push,用于标识令牌用途 - Expiration:有效期,推荐90天(不建议选择No expiration,存在安全风险)
- Note:备注,例:
- 权限勾选【核心】:勾选最上层 ✅ repo(Full control of private repositories) ,勾选后下方所有repo子权限会自动全部选中,仅勾选public_repo无法访问私有仓库
- 滚动到页面最下方,点击【Generate token】生成令牌
- ⚠️ 重要:立刻复制ghp_开头的令牌字符串保存到记事本,页面刷新后令牌将永久不可查看,丢失需要重新生成。
执行结果
- GitHub上存在私有仓库
dropxl-automation-testing-all,仓库为空; - 拿到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凭据保留不删除,实现多账号共存。
- 在PowerShell执行下面命令,快速打开Windows凭据管理器
powershell
control keymgr.dll
命令作用 :control keymgr.dll 唤起Windows凭据管理器窗口。
- 在凭据管理器选择【Windows凭据】→【添加Windows凭据】
-
网络地址:
git:[https://github.com](https://github.com) -
用户名:
carafeng1926 -
密码:粘贴上一步生成的GitHub PAT令牌(ghp_开头,Classic token必须勾选完整repo权限)
- 点击保存凭证。
执行结果 :凭据管理器新增一条记录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账号。
预期结果 :控制台输出账号列表,包含candyjing1、carafeng1926两个账号,代表两个账号凭据都已存在,多账号可共存。
步骤四:修改仓库远程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
命令作用
git remote set-url origin:修改本地仓库origin远程仓库地址;URL增加carafeng1926@,GCM会自动匹配carafeng1926凭据。git remote -v:查看远程仓库fetch、push地址,确认地址修改生效。git ls-remote origin:仅做仓库连通和鉴权校验,不会上传代码,提前验证账号、PAT、仓库地址是否全部正常。
预期结果
-
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) -
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
git push -u origin main:把本地main分支推送到远程origin仓库;-u建立本地main和远程origin/main的上下游绑定,后续直接执行git push即可推送。
预期结果
- 作者配置完成后,后续git commit提交记录作者为carafeng1926;
- git push执行成功,代码上传至
dropxl-automation-testing-all远程仓库,可在GitHub网页查看上传代码。