简体   繁体   English

如何更改Git bash中的git账户?

[英]How to change git account in Git bash?

I have weird problem with Git bash.我对 Git bash 有奇怪的问题。 I have two Github accounts, let's say A and B .我有两个 Github 帐户,比如说AB I set my name and email, like in account A:我设置了我的名字和 email,就像在账户 A 中一样:

git config --global user.name 
git config --global user.email 

I initialized new repo, did a commit, then push and git bash asked me about login and password to github.我初始化了新的 repo,做了一个提交,然后推送和git bash询问我有关 github 的登录名和密码。 By mistake I put login and password for account B, I logged in successfully.我错误地输入了帐户B的登录名和密码,我成功登录。 but there is no repository I just initialized.但没有我刚刚初始化的存储库。

How can I logout and login to different github account?如何注销并登录到不同的 github 帐户? It's not user.name nor user.email它不是user.name也不是user.email

Much simpler, as I explained in " How to sign out in Git Bash console in Windows? ":简单得多,正如我在“如何在 Windows 中的 Git Bash 控制台中注销? ”中所述:

git credential-manager delete <url>

Here这里

git credential-manager delete https://github.com

No need to remove the credential helper which is practical for caching user's password.无需删除可用于缓存用户密码的凭据助手。

My situation is I had change my gitlab.com's account email, then my local git repository can not push.我的情况是我更改了 gitlab.com 的帐户电子邮件,然后我的本地 git 存储库无法推送。 saTya 's answer worked, but in windows 10 1903, it is Control Panel -> Credential Manager -> Windows Credentials -> Generic Credentials . saTya 的回答有效,但在 Windows 10 1903 中,它是Control Panel -> Credential Manager -> Windows Credentials -> Generic Credentials

To update your Git credentials, go to Control Panel -> Credential Manager -> Generic Credentials.要更新您的 Git 凭证,请转至控制面板 -> 凭证管理器 -> 通用凭证。 Find the credentials related to your git account and edit them to use the updated password.找到与您的 git 帐户相关的凭据并编辑它们以使用更新后的密码。

Ref Link: https://cmatskas.com/how-to-update-your-git-credentials-on-windows/参考链接: https : //cmatskas.com/how-to-update-your-git-credentials-on-windows/

In windows search for Manage window credentials在 Windows 中搜索管理窗口凭据

This window will get opened这个窗口将被打开

in that look into generic credentials for git login , Then remove it by expanding it and pressing remove .在那查看git login 的通用凭据,然后通过扩展它并按remove将其删除 After try to push your code via git bash, it will automatically ask you to login .so you can login with your another account .尝试通过 git bash推送您的代码后,它会自动要求您登录。因此您可以使用另一个帐户登录 hope its helpful.希望它有帮助。

If you're already connect to any github account after some time you went to pull or push some other repository which belong to some other account.如果您在一段时间后已经连接到任何 github 帐户,则您去拉取或推送属于某个其他帐户的其他存储库。

Example:例子:
I have already connected or my github account username and password is verifed which is "account@gmail.com" it work and find everything is okay, but if I want another directory which is belong to some other account.我已经连接或验证了我的 github 帐户用户名和密码,即“account@gmail.com”它可以工作并发现一切正常,但是如果我想要另一个属于其他帐户的目录。 as I pull it.当我拉它时。 gitbash generates an error "your required repository is not found". gitbash 生成错误“找不到您所需的存储库”。 It was actually due to connecting your gitbash to an old account.这实际上是由于将您的 gitbash 连接到旧帐户。 First of all you have remove all old credentials and add new account than it will work fine首先,您已删除所有旧凭据并添加新帐户,这样就可以正常工作

example:例子:
to rmove old credational used this command使用此命令删除旧凭证

git credential-manager delete https://github.com

and add again username user name and email并再次添加用户名用户名和电子邮件

git config user.name = "new_username"
git config user.email= "newEmail@gmail.com"

after the verification push or pull you repository在验证推送或拉取您的存储库后

git pull -u origin master

that it will work fine and your code will push or pull my remote repository which is belong to other account.它会正常工作,并且您的代码将推送或拉取我属于其他帐户的远程存储库。

Change username and email global更改用户名和电子邮件全局

git config --global user.name "<username>"
git config --global user.email "<email>"

Change username and email for current repo更改当前存储库的用户名和电子邮件

git config  user.name "<username>" --replace-all
git config  user.email "<email>" --replace-all

git credentials will be searched for ~/.git-credentials or ~/.config/git/credentials files.将在 git 凭据中搜索~/.git-credentials~/.config/git/credentials文件。 You can search these files and if found then modify it.您可以搜索这些文件,如果找到则修改它。

$ git config --global --unset credential.helper

# search file
$ sudo find / -type f -name .git-credentials
$ sudo find / -type f -name credentials

For Windows , manager stores your credentials.对于Windowsmanager存储您的凭据。 It has a Control Panel Interface where you can edit or delete your stored credential.它有一个Control Panel Interface ,您可以在其中edit or delete您存储的凭据。

$ git config --global credential.helper manager

One solution: change SSH key.一种解决方案:更改 SSH 密钥。

At the Begin, I have an account A. Then, I have a ssh key on ~/.ssh/id_rsa.pub .一开始,我有一个帐户 A。然后,我在~/.ssh/id_rsa.pub上有一个 ssh 密钥。 I add this key to GitHub ssh key list https://github.com/settings/keys .我将此密钥添加到 GitHub ssh 密钥列表https://github.com/settings/keys

When I try to push commit to GitHub in CLI, the GitHub will know who I am.当我尝试在 CLI 中向 GitHub 推送提交时,GitHub 会知道我是谁。

Now, I want to switch my git account for GitHub.现在,我想为 GitHub 切换我的 git 帐户。 I just add transfer ~/.ssh/id_rsa.pub to my account B in GitHub settings.我只是在 GitHub 设置中将 transfer ~/.ssh/id_rsa.pub添加到我的帐户 B。

After this, when I try to push to GitHub, GitHub will think I am B.在这之后,当我尝试推送到 GitHub 时,GitHub 会认为我是 B。

If you are not able to clone from repo saying git clone getting remote repository not found even if the repo exist in git bash.如果您无法从 repo 克隆说 git clone 找不到远程存储库,即使该 repo 存在于 git bash 中。 Here you just need to delete the old credential and then try to access the repo with proper repo account access credentials.在这里,您只需要删除旧凭据,然后尝试使用正确的 repo 帐户访问凭据访问 repo。

git credential-manager delete https://github.com ( deleteing old credential) git credential-manager delete https://github.comgit credential-manager delete https://github.com旧凭证)

In repo url add <username>@ before github.com在 repo url 中在 github.com 之前添加<username>@

git clone --branch https://<username>@github.com/abhinav/myproj.git

Now you will get a login popup so provide the credentials现在您将看到一个登录弹出窗口,因此请提供凭据

Then cloning will be performed successfully.然后克隆将成功执行。

I have two github accounts.我有两个 github 帐户。 One for hobby one for work.一种是爱好,一种是工作。

I generated another ssh key for work: ~/.ssh/.id_rsa_work.pub我为工作生成了另一个 ssh 密钥: ~/.ssh/.id_rsa_work.pub
then add it to my work account: https://github.com/jack .然后将其添加到我的工作帐户: https://github.com/jack

In my ~/.ssh/config , now I added this for work account:在我的~/.ssh/config中,现在我为工作帐户添加了这个:

Host github-work
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_work

Then when I want to clone from company repo git@github.com:company_org_name/company_repo_name.git ,然后,当我想从公司git@github.com:company_org_name/company_repo_name.git克隆时,
I run(note the github-work part):我运行(注意github-work部分):

$ git clone git@github-work:company_org_name/company_repo_name.git

That's it.而已。

Don't forget to set local name and email for company repo right after clone.克隆后不要忘记为公司 repo 设置本地名称和 email。

git config user.name "my name at company"
git config user.email "my email at company"

Now you have multiple accounts on your device.现在您的设备上有多个帐户。 You won't feel any difference from now on.从现在开始你不会觉得有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM