blog icon indicating copy to clipboard operation
blog copied to clipboard

[Git] 使用不同账号将本地内容推送到 Github

Open yangruihan opened this issue 4 years ago • 1 comments

使用不同账号将本地内容推送到 Github

问题描述

我有2个 Github 账号,在提交不同的库时,想使用不同的 Github 账号

解决方案

方式一 ssh 方式使用 config

如果仓库使用的是 ssh,切对应 Github 账号都配置了密钥,那么可以使用如下命令,对不同的仓库设置不同的用户名和邮箱地址

git config user.name her_username
git config user.email her_email

使用这种方法,会将提交日志上的用户名和邮箱也更改

如果使用的是 Https 方式,那么这种方法不适用于 Push

现在 Github 默认 clone 的方式是 Https,因此该方法有一定局限

方式二 https 方式清理密码管理器

git config --local credential.helper ""
git push origin master

macOS下需要去 Keychain Access 里删除 Github Login 项

清理密码管理器后,推送时,将会出现输入用户名和密码的提示,输入不同的用户名和密码即可

方式三 推送时附带用户名和密码

git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git

参考地址

https://stackoverflow.com/questions/13103083/how-do-i-push-to-github-under-a-different-username

yangruihan avatar Nov 21 '20 10:11 yangruihan