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

482

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


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

605
  1. 在终端中,导航到您要进行更改的存储库。
  2. 执行 git config --list 命令以检查本地存储库中当前的用户名和电子邮件。
  3. 根据需要更改用户名和电子邮件。可以将其作为全局更改或特定于本地存储库进行更改:
    git config [--global] user.name "Full Name"
    git config [--global] user.email "email@address.com"

    对于每个存储库,您也可以手动编辑 .git/config 文件来进行更改。
  4. 完成!

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

这是 Windows 上的演示: enter image description here

有疑问? 了解更多


71
我想要指出给将来看到这篇文章的人,如果你使用 --local 参数,在同一台电脑上可以在不同的代码库中使用不同的凭据(例如,你可以使用个人仓库的凭据在工作笔记本上提交代码,但同时在其他代码库中保持工作凭据登录状态)。 - Xeraqu
4
如@DalyaG所提到的,以下内容也应包括在内:“git config credential.username "xxx"”。 - Fernando Wittmann
3
通过使用git config user.name "Full Name"设置正确的用户名后,当我通过git config --list检查我的配置时,列表中会出现两个user.name记录。前一个是不需要的,而后一个则是我通过git config命令设置的。不幸的是,如果我进行提交,将会应用错误的记录。 - webpreneur
5
对于任何新手:去掉 [ ] 括号即可获得 --global 配置。这些括号仅表示您可以选择采用此选项或不采用。 - questionto42
2
嗨@webpreneur。请查看此链接以了解为什么您有两个用户名或电子邮件:https://www.quora.com/Why-does-git-config-list-show-2-emails。基本上它是在说您有不同的配置文件,因此有“--global”,“--local”和“--system”。所以您正在查看“--global”和“--local”的内容。如果您只想查看最近添加的本地配置,请键入“git config --list --local”。 - Bernat
显示剩余3条评论

239

你可能需要更新远程URL,因为GitHub会在其中放置您的用户名。您可以通过键入以下内容查看原始URL:

git config --get remote.origin.url

或者直接前往Github上的存储库页面获取新的URL。然后使用该URL即可。

git remote set-url origin https://{new url with username replaced}

更新URL以显示您的新用户名。


@user3370902 首先确保您使用的是正确的用户名和密码。然后还要验证一下 Github 仓库页面上的 URL 是否正确。如果问题仍然存在,建议您跟进 Github 支持,因为他们可以准确地了解情况。 - Steven V
3
当我登录github.com时,用户名和密码有效 - 只是在终端中无法工作。有没有办法关闭终端,注销/关闭git,然后在终端中重新启动git? - user3370902
@user3370902 只有在命令提示符下实际执行git命令时,git才会运行。没有需要重新启动的git服务。您是否使用双重身份验证或其他奇怪的东西? - Steven V
1
@user3370902 在 https://help.github.com/articles/providing-your-2fa-security-code 页面底部的“通过命令行”部分,您需要创建一个个人访问令牌。 - Steven V
@user3370902 你应该像存储密码一样存储它。也许可以使用凭据管理器?如果你想要的话,你的操作系统中可能已经内置了一个,git可以在其中为你存储密码。 - Steven V
显示剩余5条评论

222

我们可以通过以下3种方法解决这个问题。

方法一(命令行)

要在全局范围内设置您账户的默认标识,请运行以下命令:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git config --global user.password "your password"

如果只想在当前代码库中设置身份,请删除--global并在您的项目/代码库根目录下运行以下命令:

git config user.email "you@example.com"
git config user.name "Your Name"
git config user.password "your password"

例子:

email -> organization email Id
name  -> mostly <employee Id> or <FirstName, LastName> 

注意:你可以在GitHub资料页面或Bitbucket资料页面查看这些值

方法二(.gitconfig)

如果不存在,请在主目录中创建一个.gitconfig文件,并将以下行粘贴到.gitconfig中。

[user]
    name = FirstName, LastName
    email = FirstName.LastName@company.com
    password = abcdxyz
[http]
    sslVerify = false
    proxy = 
[https]
    sslverify = false
    proxy = https://corp\\<uname>:<password>@<proxyhost>:<proxy-port>
[push]
    default = simple
[credential]
    helper = cache --timeout=360000000
[core]
    autocrlf = false

注意:如果您没有使用代理,可以从上面的内容中删除代理行。
主目录创建 .gitconfig 文件:
Windows: c/users/<用户名或员工ID> Mac 或 Linux:运行以下命令进入主目录 cd ~
或者依次运行以下命令。
git config --global --edit
git commit --amend --reset-author

方法三(Git凭据弹出窗口)

Windows:

Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential 
>> look for any github cert/credential and delete it.

运行任何git命令时都会提示输入新的用户名和密码(注意:有时您在使用git pull时不会被提示输入密码)。 Mac:
command+space >> search for "keychain Access" and click ok >> 
search for any certificate/file with gitHub >> delete it.

运行任何git命令将提示输入新的用户名和密码(注意:有时您在进行git pull操作时不需要输入密码)。


@BenyaminJafari在这些情况下,请遵循第三种方法。 - sravan ganji
实际上我是一个Linux用户。 - Benyamin Jafari
@BenyaminJafari 对于Linux,请使用第二种方法。我尝试过,对我有效。 - sravan ganji
第二个方法中的密码属性在哪里? - Benyamin Jafari
@BenyaminJafari 或者你可以将密码保存在 gitconfig 中的用户部分,但我不建议采用这种方法。 - sravan ganji
显示剩余4条评论

208
  1. 编辑:除了更改您的名称和电子邮件之外,您可能还需要更改凭据:
  • 要仅更改一个存储库的本地设置,请在终端中输入,从存储库内部开始:

git config credential.username "new_username"
  • 要全局更改,请使用

  • git config --global credential.username "new_username"
    

    (编辑解释: 如果您不更改 user.emailuser.name,您可以推送您的更改,但它们将在 git 中注册为之前的用户)



    1. 下次您执行 push 操作时,系统会要求您输入密码

      'https://<new_username>@github.com' 的密码:


    20
    谢谢!这应该是最佳答案 :) 以下内容对我有帮助: git config user.name "xxx"-> git config user.email "xxx"-> git config credential.username "xxx"` - Fernando Wittmann
    13
    全局应该放在config之后,即:git config --global credential.username "new_username" - Caleb Rotich
    如何删除 credential.username?@DalyaG - Khant
    1
    @Khant 使用 --unset,例如 git config user.email --unset - DalyaG
    2021年8月13日开始不再支持密码身份验证。 - Ishita Sinha

    32
    请更新新用户存储库 URL。
     git remote set-url origin https://username@bitbucket.org/repository.git
    

    我尝试使用以下命令,但它们不起作用:

    git config user.email "email@example.com"
    git config user.name  "user"
    

    或者

    git config --global user.email "email@example.com"
    git config --global user.name "user"
    

    14

    我建议你通过前往你的.git文件夹并打开config文件来完成此操作。在文件中粘贴你的用户信息:

    [user]
        name = Your-Name
        email = Your-email
    

    这应该就是了。


    2
    我宁愿选择这种方法而不是其他方法。简单但有效。 - Yohanim

    14

    针对这个问题有一个简单的解决方案,即从您的钥匙串中移除证书,这样会再次要求用户输入用户名和密码。

    操作步骤:

    1. 打开钥匙串访问

    enter image description here

    1. 搜索证书 gitHub.com。

    2. 删除 gitHub.com 证书。

    3. 在终端中使用 git 执行任何操作,这将再次要求您输入用户名和密码。

    对于 Windows 用户,请按照以下方式找到钥匙串:

    控制面板 >> 用户帐户 >> 凭据管理器 >> Windows 凭据 >> 通用凭据


    2
    谢谢。这个解决了问题。我找不到它来自哪里。 - Razi Kallayi
    是的,我尝试了大多数的解决方案,但只有通过凭据管理器来解决。 - MonneratRJ
    谢谢,兄弟。这个解决了问题。我找不到它来自哪里。 - Abdelrahman Mohamed Elakliby

    14

    从您的终端执行以下操作:

    git config credential.username "prefered username"
    

    或者

    git config --global user.name "Firstname Lastname"
    

    8
    **Check by executing this** 
    git config --list
    **Change user email** 
    git config --global user.email "email@example.com"
    **Change user name**
    git config --global user.name "user"
    **Change user credential name** 
    git config --global credential.username "new_username"
    **After this a window popup asking password.
    Enter password and proceed.**
    

    6

    首先,您需要从本地计算机更改凭据

    1. 如果有通用凭据,请删除

    通用凭据

    1. 配置新用户和电子邮件(如果需要可以全局配置)
    git config [--global] user.name "Your Name"
    git config [--global] user.email "email@address.com"
    
    1. 现在上传或更新您的代码库,它会要求您输入用户名和密码以获取访问GitHub的权限。

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