Skip to content

liaohaoliang2003/github-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github-switch

Windows 上的 GitHub 多账号切换工具,提供 GUI 和 CLI 两种入口。

当你同时使用个人、公司等多个 GitHub 身份时,只修改 git config 往往不够:Git Credential Manager 或 IDE 里残留的旧凭据仍可能被复用,导致 push、publish 或插件登录串号。github-switch 用一个明确的流程完成身份切换、凭据清理和可选 IDE 缓存处理。

github-switch 主要完成四项工作:

  • 切换 Git 身份配置。
  • 清理当前 GitHub HTTPS 凭据(Windows Credential Manager / Git Credential Manager 中的旧凭据)。
  • 清理当前 IDE GitHub 会话与 Publish to GitHub 发布缓存。
  • 在切换到预置账号时可应用已保存的 GitHub Token。

解决的问题

  • 公司账号、个人账号或多个 GitHub 身份之间切换时,配置步骤容易遗漏。
  • 已经修改 user.nameuser.email,但 git push 仍然使用旧 GitHub 凭据。
  • Windows Credential Manager / Git Credential Manager 中的旧凭据需要手动清理。
  • VS Code、Cursor、Trae 的 GitHub 登录状态或 Publish to GitHub 发布缓存仍显示旧账号。
  • GitHub Token 需要本地安全保存,同时又要能在需要时查看或复制。

核心功能

  • 切换 Git 配置:user.nameuser.emailcredential.https://github.com.username
  • 清理 GitHub 凭据:仅处理目标包含 github.com 的 Git / GCM / Windows Credential Manager 条目。
  • 支持作用域:global 全局配置,或 local 指定仓库配置。
  • 支持预置账号:账号信息保存在本地 profiles/accounts.json
  • 支持临时账号:不保存账号档案,直接按输入内容切换。
  • 支持 GitHub Token:Token 本地加密保存,切换到对应预置账号时可自动应用。
  • 支持 IDE GitHub 会话与 Publish to GitHub 发布缓存清理:覆盖 VS Code、Cursor、Trae / Trae CN;GUI 默认勾选,可手动取消;CLI 仍需显式传参。
  • 支持 GUI 与 CLI:GUI 适合日常使用,CLI 适合脚本和自动化场景。

快速开始

推荐流程

  1. 新增或编辑预置账号。
  2. 为需要自动认证的账号保存 Token。
  3. 选择预置账号或临时输入账号,执行切换。
  4. 如果 IDE 或 Publish to GitHub 仍显示旧账号,可执行 IDE 会话与发布缓存清理。

首次启动时,如果 profiles/accounts.json 不存在,工具会自动准备本地配置文件;如果 profiles/profiles/accounts.example.json 也不存在,会使用内置默认示例一并创建并提示。

方式一:GUI EXE 启动

适合日常使用。

可以直接双击 github-switch-gui.exe 启动 GUI。

也可以使用 PowerShell 启动 GUI:

.\github-switch-gui.exe

GUI EXE 内置默认资源,不需要外置 ui/ 或示例配置文件即可启动。首次运行时会在 EXE 同级目录自动准备本地配置:

  • profiles/accounts.example.json
  • profiles/accounts.json

方式二:GUI ps1 启动

适合源码调试或不想使用 EXE 的场景:

在 PowerShell 中执行以下命令:

powershell -ExecutionPolicy Bypass -File .\ui\Start-GitHubSwitchGui.ps1

源码方式会读取仓库内的 ui/strings.zh-CN.json,方便开发时维护中文文案。

方式三:CLI 启动

适合命令行、脚本或自动化场景。

源码(ps1)方式:

powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Profile work

EXE 方式使用相同参数:

.\github-switch-cli.exe -Profile work

也可以直接双击 github-switch-cli.exe 启动 CLI。

GUI 使用

GUI 入口:

.\github-switch-gui.exe

源码入口:

powershell -ExecutionPolicy Bypass -File .\ui\Start-GitHubSwitchGui.ps1

GUI 支持以下操作:

  • 选择预置账号并切换。
  • 新增或编辑预置账号。
  • 保存、查看、复制或删除预置账号 Token。
  • 使用临时输入账号切换。
  • 可选清理 VS Code 系 IDE GitHub 会话与 Publish to GitHub 发布缓存。
  • 选择 globallocal 作用域。
  • local 模式下选择目标仓库路径。

如果 profiles/accounts.json 不存在,GUI 会自动创建本地配置文件;如果 profiles/profiles/accounts.example.json 也不存在,会使用内置默认示例一并创建。

常用命令

场景 命令
使用预置账号切换 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Profile work
使用临时账号切换 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Name "Alice" -Email "alice@example.com" -Username "alice"
切换当前仓库配置 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Profile work -Scope local -RepoPath .
初始化本地配置 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -InitializeProfiles
保存预置账号 Token powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -SetProfileToken work -Token <token>
查看 Token 状态 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ShowProfileTokenStatus work
显式查看 Token powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ShowProfileToken work
查看 IDE GitHub 会话状态 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ShowIdeGitHubSessionStatus
仅清理 IDE 会话/发布缓存 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ClearIdeGitHubSessionOnly
切换时同时处理 IDE 会话/发布缓存 powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Profile work -ClearIdeGitHubSession

EXE 版 CLI 参数相同,将命令开头替换为 .\github-switch-cli.exe 即可。示例:

.\github-switch-cli.exe -Profile work -ClearIdeGitHubSession

IDE 中仍显示旧 GitHub 账号怎么办

VS Code、Cursor、Trae 等 VS Code 系 IDE 会维护自己的 GitHub 登录会话。IDE 登录状态和 Git 凭据是两套机制:

  • Git / Git Credential Manager 决定命令行 Git HTTPS 操作使用哪个 GitHub 凭据。
  • IDE 的 GitHub、Copilot、Settings Sync 或插件登录状态由 IDE 自己管理,可能不会随着 Git 凭据清理自动变化。

因此,如果切换后 IDE 右下角、账号菜单或插件仍显示旧账号,可以使用可选 IDE 会话处理:

powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -Profile work -ClearIdeGitHubSession

也可以只查看或单独清理 IDE 会话:

powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ShowIdeGitHubSessionStatus
powershell -ExecutionPolicy Bypass -File .\scripts\switch-git-account.ps1 -ClearIdeGitHubSessionOnly

GUI 中也提供“同时处理 VS Code 系 IDE GitHub 会话与发布缓存”和“清理 IDE 会话/发布缓存”入口。其中切换账号旁的复选框为 GUI 默认勾选;如果不想处理 IDE 登录状态,可以手动取消。CLI 仍通过 -ClearIdeGitHubSession-ClearIdeGitHubSessionOnly 显式触发。

注意:工具只会尝试清理可明确识别的 IDE GitHub 认证凭据与发布缓存,不删除 IDE 配置、扩展数据或工作区数据。清理后建议重启 IDE,并在 GitHub / Copilot / GitHub 插件要求时重新登录目标账号。

Publish to GitHub 仍显示旧账号

如果你点击源码管理里的 “Publish to GitHub” 后,候选项仍显示旧账号,这通常来自 VS Code 系 IDE 的发布缓存,而不是 Git/GCM 凭据。

VS Code 系 IDE 会把 GitHub 发布入口的一些状态写入 state.vscdb,其中可能包含 vscode.github-github 这样的缓存键。工具在你显式执行 IDE 清理时,会扫描 VS Code、Cursor、Trae / Trae CN 的 User/globalStorage/state.vscdbUser/workspaceStorage/*/state.vscdb,只删除白名单内的发布缓存键,并在修改前创建备份。

建议流程:

  1. 关闭 VS Code / Cursor / Trae。
  2. 运行 .\github-switch-cli.exe -ClearIdeGitHubSessionOnly -IdeTarget trae,或在 GUI 中点击“清理 IDE 会话/发布缓存”。
  3. 重新打开 IDE,再点击 “Publish to GitHub”。此时 IDE 应要求重新登录或重新选择目标 GitHub 账号。

Token 安全

本工具不保存账号密码,只支持 GitHub Token。

  • Token 写入本地 profiles/accounts.json
  • 文件中保存的是 Windows DPAPI(CurrentUser)密文,不是明文 Token。
  • Token 只关联预置账号,不作用于临时输入模式。
  • 默认只显示 Token 状态,不显示 Token 明文。
  • 只有主动查看、复制或执行 -ShowProfileToken 时,才会解密并返回原始 Token。

文件提交规则:

  • profiles/accounts.json 已被 .gitignore 忽略。
  • profiles/accounts.example.json 是示例文件,不应包含真实账号或 Token。
  • 根目录 EXE 是本地构建产物,也已被 .gitignore 忽略。

如果曾经把明文 Token 提交到 Git 仓库,应立即在 GitHub 中删除或轮换该 Token。

EXE 构建与运行

构建命令:

powershell -ExecutionPolicy Bypass -File .\scripts\build-exe.ps1

构建结果位于项目根目录:

  • github-switch-gui.exe
  • github-switch-cli.exe

EXE 内置默认资源和 PowerShell 模块逻辑,不依赖外部 .ps1.psm1ui/ 或示例配置文件。最小发布包可以只包含:

  • github-switch-gui.exe
  • github-switch-cli.exe

首次运行后,工具会在 EXE 同级目录自动生成 profiles/accounts.example.jsonprofiles/accounts.json。其中 profiles/accounts.json 是本地私有配置,用于保存真实账号信息和 DPAPI 加密后的 Token。

配置文件

文件 说明
profiles/accounts.example.json 示例配置文件,可提交到仓库;EXE 模式下缺失时会自动生成。
profiles/accounts.json 本地真实配置文件,保存预置账号和加密 Token,不应提交。

profiles/accounts.json 不存在时,GUI / CLI 会自动准备;也可以通过 -InitializeProfiles 显式创建。

开源前检查

发布仓库前建议确认:

  • profiles/accounts.json 未被提交。
  • profiles/accounts.example.json 不包含真实姓名、邮箱、GitHub 用户名或 Token。
  • github-switch-gui.exegithub-switch-cli.exe 未被提交。
  • Git 历史中没有提交过明文 Token;如有,应轮换 Token。

License

本项目使用 MIT License。详情见 LICENSE

限制与注意事项

  • v1 仅面向 Windows。
  • 依赖 PowerShell、Git for Windows 和 Windows Credential Manager。
  • Token 加密依赖 Windows DPAPI CurrentUser,通常只能由创建该密文的 Windows 用户解密。
  • 不管理 SSH key。
  • 不自动执行 gh auth login
  • 不向 profiles/accounts.example.json 写入 Token。
  • 内部兼容标识仍为 GitHubSwitch,既有本地 Token 和凭据前缀不需要迁移。

About

Windows 上的 GitHub 多账号一键快速切换工具,支持 GUI/CLI、凭据清理、Token 加密保存与 VS Code 系 IDE 发布缓存处理。

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors