【问题标题】:How to change my Git username in terminal?如何在终端中更改我的 Git 用户名?
【发布时间】:2014-05-15 16:55:18
【问题描述】:

我在终端中推拉 git,然后我在 github.com 上更改了我的用户名。我去推送一些更改,但它无法推送,因为它仍在识别我的旧用户名。我如何在终端的 git 上更改/更新我的用户名?

【问题讨论】:

    标签: git github git-config


    【解决方案1】:

    您可能需要更新远程 URL,因为 github 将您的用户名放入其中。您可以通过键入

    查看原始 URL
    git config --get remote.origin.url
    

    或者直接访问 Github 上的存储库页面并获取新 URL。然后使用

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

    用您的新用户名更新 URL。

    【讨论】:

    • 好的,我检查了原始 url,它使用的是旧用户名,我继续更新 url 以反映我所在的新用户名和存储库,然后它要求我输入用户名和密码.我输入了我的凭据,上面写着致命的:远程身份验证失败:用户名和密码无效。我检查了 github.com 并用我的帐户签名,所以我知道这些凭据是正确的......有什么想法吗?
    • @user3370902 我会确保您首先使用正确的用户名和密码。然后还使用 github 存储库页面验证您的 URL 是否正确。如果您仍然遇到问题,您可能需要跟进Github Support,因为他们将能够确切地看到发生了什么。
    • 当我登录到 github.com 时,用户名和密码有效——只是在终端中不起作用。有没有办法关闭终端,注销/关闭 git,然后在终端中重新启动 git?
    • @user3370902 git 仅在您在命令提示符下实际执行 git 命令时运行。没有需要重新启动的 git 服务。您是否使用两因素身份验证或任何其他奇怪的东西?
    • @user3370902 help.github.com/articles/providing-your-2fa-security-code 在“通过命令行”下的底部附近。您需要创建个人访问令牌。
    【解决方案2】:
    1. 在您的终端中,导航到要在其中进行更改的存储库。
    2. 执行git config --list 以检查您本地存储库中的当前用户名和电子邮件。
    3. 根据需要更改用户名和电子邮件。使其成为全局更改或特定于本地存储库:
      git config [--global] user.name "Full Name"
      git config [--global] user.email "email@address.com"

      您也可以根据 repo 手动编辑 .git/config
    4. 完成!

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

    这是它在 Windows 上的外观

    疑难解答? Learn more

    【讨论】:

    • 我想为以后看到这个的人指出,如果您使用--local,您可以在同一台计算机上的特定存储库中使用不同的凭据。 (例如,您想从您的工作笔记本电脑提交您的个人仓库,但在其他仓库中使用您的工作凭据登录。)
    • @DalyaG 提到,还应包括以下内容:git config credential.username "xxx"
    • 因为“手动编辑 .git/config”而投票。在我的情况下,更改 remote.origin.url 是最好的选择
    • 在使用git config user.name "Full Name" 设置我的正确用户名之后。当我通过git config --list 检查我的配置时,列表中有两个 user.name 记录。前者是不需要的,后者是我通过 git config 命令设置的。可悲的是,如果我进行提交,则会应用错误的提交。
    • 对于任何新手:删除[ ] 括号以获取--global 配置。方括号只表示您可以决定是否接受。
    【解决方案3】:

    请更新新的用户存储库 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"
    

    【讨论】:

      【解决方案4】:
      1. 编辑:除了更改您的姓名和电子邮件,您可能还需要更改您的凭据:
      • 要仅在本地更改一个存储库,请从存储库中输入终端

        git config credential.username "new_username"
        
      • 改变全局使用

        git config --global credential.username "new_username"
        

        (EDIT EXPLAINED:如果您不更改user.emailuser.name,您将能够推送您的更改,但它们将在前一个用户下的 git 中注册)

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

        Password for 'https://<new_username>@github.com':

      【讨论】:

      • 谢谢!这应该在最佳答案中:)以下为我解决了:`git config user.name "xxx" -> git config user.email "xxx" -> git config credential.username "xxx"
      • 全球应该在 config git config --global credential.username "new_username" 之后
      • 感谢@CalebRotich!相应地更新了我的答案。
      • 如何删除 credential.usernane? @DalyaG
      • @Khant 使用--unset,例如git config user.email --unset
      【解决方案5】:

      我建议您只需转到 .git 文件夹,然后打开 config 文件即可。在文件中粘贴您的用户信息:

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

      应该就是这个了。

      【讨论】:

      • 我宁愿选择这种方法而不是其他方法。简单但完成。
      【解决方案6】:

      有 3 种方法可以解决此问题

      method-1(命令行)

      要设置您帐户的默认身份globally,请运行以下命令

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

      要仅在当前存储库中设置身份,请删除 --global 并在您的 Project/Repo 根目录中运行以下命令

      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 个人资料中查看这些值

      方法 2 (.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/

      Mac 或 Linux: 运行此命令进入主目录cd ~

      或者干脆一个接一个地运行以下命令

      git config --global --edit
      git commit --amend --reset-author
      

      method-3(弹出git凭证)

      窗口:

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

      然后运行任何 git 命令将提示输入新用户名和 密码。

      苹果机:

      command+space >> search for "keychain Access" and click ok >> 
      search for any certificate/file with gitHub >> delete it.
      

      然后运行任何 git 命令将提示输入新用户名和 密码。

      【讨论】:

      • 即使添加了git config user.namegit config user.password,在任何push 之后都需要用户名和密码
      • @BenyaminJafari 在这些情况下请遵循第三种方法
      • 其实我是Linux用户
      • @BenyaminJafari for linux 使用第二种方法。我试过它对我有用
      • 第二种方法的密码属性在哪里?
      【解决方案7】:

      如果你已经创建了一个新的 Github 账户,并且你想用你的新账户而不是你以前的账户推送提交,那么 .gitconfig 必须更新,否则,你将使用已经拥有的 Github 账户推送到新账户。

      为了解决这个问题,您必须导航到您的主目录并使用编辑器打开 .gitconfig。编辑器可以是vim,notepad++,甚至notepad。

      打开 .gitconfig 后,只需使用您要推送的新 Github 帐户用户名修改“名称”。

      【讨论】:

        【解决方案8】:

        这个问题有一个简单的解决方案,解决方案是删除你的钥匙串的证书,之前的事情会导致它再次询问用户和密码。

        步骤:

        1. 打开钥匙串访问

        1. 搜索证书 gitHub.com。

        2. 删除 gitHub.com 证书。

        3. 在终端中使用 git 执行任何操作。这再次询问您的用户名和密码。

        对于 Windows 用户,通过以下方式查找密钥链:

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

        【讨论】:

        • 谢谢。这个解决了这个问题。我找不到它的来源。
        • 是的,我尝试了大多数解决方案,但只有通过凭证管理器解决了
        【解决方案9】:

        从您的终端执行:

        git config credential.username "prefered username"
        

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

        【讨论】:

          【解决方案10】:

          通常用户名位于 git config 下

          git config --global user.name "first last"
          

          虽然如果你仍然看到上面不起作用,你可以在你的 mac 用户目录下编辑 .gitconfig 并更新

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

          【讨论】:

            【解决方案11】:

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

            1. 删除通用凭据(如果有)

            1. 配置新用户和电子邮件(如果需要,可以全局设置)
            git config [--global] user.name "Your Name"
            git config [--global] user.email "email@address.com"
            
            1. 现在上传或更新您的 repo,它会询问您的用户名和密码以访问 github

            【讨论】:

              【解决方案12】:

              如果您使用包含您的用户名的 url 克隆了您的 repo,那么您还应该更改 remote.origin.url 属性,否则它会一直询问旧用户名的密码。

              示例:

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

              应该改为

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

              【讨论】:

                【解决方案13】:
                **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.**
                

                【讨论】:

                  【解决方案14】:
                  1. 在终端中,导航到要在其中进行更改的存储库。
                  2. 执行 git config --list 以检查本地存储库中的当前用户名和电子邮件。
                  3. 根据需要更改用户名和电子邮件。使其成为全局更改或特定于本地存储库:

                  git config [--global] user.name "Full Name"

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

                  您也可以根据 repo 手动编辑 .git/config

                  1. 完成!

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

                  【讨论】:

                    【解决方案15】:

                    您可以在终端中使用此命令更改当前 git 用户

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

                    【讨论】:

                    • 这在其他较早的答案中已经提到过。
                    【解决方案16】:

                    我自己最近也遇到了同样的问题。就我而言,我有两个 github 帐户:工作帐户和个人帐户。我想将我的起始代码推送到我的个人帐户中的存储库,但全局配置有我的工作帐户。我不想每次不得不在工作和个人项目之间切换时都重新配置全局。所以我使用这些命令为那个特定的个人项目文件夹设置我的用户名和电子邮件。

                    解决办法:

                    $ git config user.name "Alex"

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

                    $ git config credential.username "your_account_name_here"

                    【讨论】:

                      【解决方案17】:

                      在 linux (Ubuntu 18.04) 中,are saved as cleartext 文件中的用户名/密码 ~/.git-credentials,只需编辑文件以使用您的新用户名/密码。

                      文件格式非常简单,每一行都包含一个用户/域的凭据,格式如下:

                      https://<username>:<password>@github.com
                      https://<username2>:<password2>@bitbucket.com
                      ...
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2019-10-16
                        • 2021-12-18
                        • 2012-09-02
                        • 1970-01-01
                        • 2019-02-19
                        • 2011-12-10
                        • 2020-10-22
                        • 2016-10-01
                        相关资源
                        最近更新 更多