如何在终端中更改我的Git用户名?

482

我在终端中使用git进行推送和拉取,然后我在github.com上更改了我的用户名。当我尝试推送一些更改时,由于仍然识别我的旧用户名,因此无法进行推送。如何在终端中更改/更新我的git用户名?


我认为这里可能会有用的是如何设置每个项目的gitconfig问题 - Scrooge McDuck
请查看https://builditmasters.com/git-config-username-and-password-global/。 - Osanda Gamage
18个回答

6

针对 Github

git config --local user.name "another_username" 
git config --local user.email "email@example.com"
git remote set-url origin https://another_username@github.com/repo_url

4
  1. 在终端中导航到您想要进行更改的仓库。
  2. 执行 git config --list 命令,检查本地仓库中当前的用户名和电子邮件地址。
  3. 根据需要更改用户名和电子邮件地址。可以全局更改或仅更改本地仓库:

git config [--global] user.name "姓名"

git config [--global] user.email "email@address.com"

对于每个仓库,您还可以手动编辑 .git/config 文件。

  1. 完成!

在执行第2步时,如果看到 credential.helper=manager,您需要打开计算机的凭据管理器 (Windows 或 Mac),并在其中更新凭据。


3

我最近也面临了同样的问题。在我的情况下,我有两个 GitHub 账户:工作和个人账户。我想将我的起始代码推送到个人账户的仓库中,但是全局配置使用的是我的工作账户。我不想每次切换工作和个人项目时都要重新配置全局设置。因此,我使用以下命令为该特定的个人项目文件夹设置了用户名和电子邮件。

解决方案:

$ git config user.name "Alex"

$ git config user.email "Alex@example.com"

$ git config credential.username "your_account_name_here"


1
在Linux(Ubuntu 18.04)中,用户名/密码以明文形式保存在文件~/.git-credentials中,只需编辑该文件以使用新的用户名/密码。
文件格式非常简单,每行包含一个用户/域的凭据,格式如下:
https://<username>:<password>@github.com
https://<username2>:<password2>@bitbucket.com
...

1
通常用户名存储在git配置文件中。
git config --global user.name "first last"

如果您仍然发现上述方法不起作用,您可以编辑位于Mac用户目录下的.gitconfig文件并进行更新。

[user]
        name = gitusername
        email = xyz@xyz.com

0

如果您已经创建了一个新的Github账户,并且想要使用新账户而不是之前的账户推送提交,则必须更新.gitconfig文件,否则您将使用已拥有的Github账户向新账户推送。

为了解决这个问题,您需要进入您的主目录并使用编辑器打开.gitconfig文件。编辑器可以是vim、notepad++或者甚至是记事本。

一旦您打开了.gitconfig文件,只需使用您想要推送的新Github账户用户名修改“name”即可。


0
如果您使用包含您的用户名的URL克隆了存储库,则还应更改remote.origin.url属性,否则它会继续要求旧用户名的密码。
例如:
remote.origin.url=https://<old_uname>@<repo_url>

应该改为

remote.origin.url=https://<new_uname>@<repo_url>

0
在我的情况下,错误是:
ERROR: Permission to <repo>.git denied to <username_2>.
fatal: Could not read from remote repository.

这是由于ssh代理处于不良状态引起的。有所帮助的是从代理中删除旧密钥。
ssh-add -d ~/.ssh/<username_2_key>

添加一个有效的密钥:
ssh-add -d ~/.ssh/<username_1_key>

编辑:在第一步中,你可能想尝试使用ssh-add -D命令来删除所有身份标识。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接