很多公司都是企业组织在 Github 上进行代码的管理,因此大家可能需要一个工作的账户和个人的账户区分。其实这个非常好弄,只需要两部即可:
Step1 配置多份配置
你可以在你的根目录,创建下面的的结构
~
├── .gitconfig <-- global
└── Developer/
├── personal/
│ ├── project_1/
│ ├── project_2/
│ ├── project_#/
│ └── .gitconfig <-- personal
└── company/
├── project_1/
├── project_2/
├── project_#/
└── .gitconfig <-- company
然后更新两份配置
# ~/Developer/personal/.gitconfig
[credential]
username = <github-user>
[user]
name = <github-user>
email = <github-user>@users.noreply.github.com
# ~/Developer/company/.gitconfig
[credential]
username = <user>
[user]
name = <First Name and Last Name>
email = <user>@company.org
最后更新下 global gitconfig
# ~/.gitconfig
[includeIf "gitdir/i:~/Developer/personal/"]
path = ~/Developer/personal/.gitconfig
[includeIf "gitdir/i:~/Developer/company/"]
path = ~/Developer/company/.gitconfig
使用 HTTPS 和个人访问令牌为多个账户提交代码
打开你的终端:
git config --get credential.helper
如果你使用 macOS
git credential -osxkeychain erase https://github.com
接着再配置:
git config --global credential.https://github.com.useHttpPath true
扩展阅读
- https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-your-personal-account/merging-multiple-personal-accounts
- https://medium.com/nerd-for-tech/workflow-for-switching-github-accounts-in-your-terminal-d87e50bb5511
- https://dev.to/equiman/how-to-use-multiple-users-with-git-2e9l
- https://dev.to/0xbf/switch-github-account-in-terminal-92g