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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22844806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:57:50  来源:igfitidea点击:

How to change my Git username in terminal?

gitgithubgit-config

提问by user3370902

I was pushing and pulling from git in Terminal then I changed my username on github.com. I went to push some changes and it couldn't push because it was still recognizing my old username.. How do I change/update my username on git in terminal?

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

采纳答案by Steven V

You probably need to update the remote URL since github puts your username in it. You can take a look at the original URL by typing

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

git config --get remote.origin.url

Or just go to the repository page on Github and get the new URL. Then use

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

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

to update the URL with your new username.

使用您的新用户名更新 URL。

回答by Oriol

  1. In your terminal, navigate to the repo you want to make the changes in.
  2. Execute git config --listto check current username & email in your local repo.
  3. Change username & email as desired. Make it a global change or specific to the local repo:
    git config [--global] user.name "Full Name"
    git config [--global] user.email "[email protected]"

    Per repo basis you could also edit .git/configmanually instead.
  4. Done!
  1. 在您的终端中,导航到您要进行更改的存储库。
  2. 执行git config --list以检查本地存储库中的当前用户名和电子邮件。
  3. 根据需要更改用户名和电子邮件。使其成为全局更改或特定于本地存储库:
    git config [--global] user.name "Full Name"
    git config [--global] user.email "[email protected]"

    您也可以.git/config手动编辑每个存储库。
  4. 完毕!

When performing step 2if you see credential.helper=manageryou need to open the credential manager of your computer (Win or Mac) and update the credentials there

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

Here is how it look on windows enter image description here

这是它在 Windows 上的外观 在此处输入图片说明

Troubleshooting? Learn more

故障排除?了解更多

回答by DalyaG

  1. EDIT: In addition to changing your name and emailYou may also need to change your credentials:

    • To change locally for just one repository, enter in terminal, from within the repository

      git config credential.username "new_username"

    • To change globally use

      git config credential.username --global "new_username"

    (EDIT EXPLAINED: If you don't change also the user.emailand user.name, you will be able to push your changes, but they will be registered in git under the previous user)

  2. Next time you push, you will be asked to enter your password

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

  1. 编辑:除了更改您的姓名和电子邮件之外,您可能还需要更改您的凭据:

    • 要仅在本地更改一个存储库,请从存储库中的终端中输入

      git config credential.username "new_username"

    • 更改全局使用

      git config credential.username --global "new_username"

    编辑解释:如果您不更改user.emailand user.name,您将能够推送您的更改,但它们将在前一个用户的 git 中注册)

  2. 下次push您将被要求输入密码

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

回答by sravan ganji

To set your account's default identity globallyrun below commands

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

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git config --global user.password "your password"

To set the identity only in current repository , remove --globaland run below commands in your Project/Repo root directory

要仅在当前存储库中设置标识,请删除--global并运行项目/存储库根目录中的以下命令

git config user.email "[email protected]"
git config user.name "Your Name"
git config user.password "your password"

Example:

例子:

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

**Note: ** you can check these values in your GitHub profile or Bitbucket profile

**注意:**您可以在您的 GitHub 个人资料或 Bitbucket 个人资料中检查这些值

回答by Raja Rama Mohan Thavalam

Please update new user repository URL

请更新新的用户存储库 URL

 git remote set-url origin https://[email protected]/repository.git

I tried using below commands, it's not working:

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

git config user.email "[email protected]"
git config user.name  "user"

OR

或者

git config --global user.email "[email protected]"
git config --global user.name "user"

回答by Emmanuel Benson

From your terminal do:

从你的终端做:

git config credential.username "prefered username"

OR

或者

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

回答by Fredy Gonzalo Captuayo Novoa

There is a easy solution for that problem, the solution is removed the certificate the yours Keychain, the previous thing will cause that it asks again to the user and password.

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

Steps:

脚步:

  1. Open keychain access
  1. 打开钥匙串访问

enter image description here

在此处输入图片说明

  1. Search the certificate gitHub.com.

  2. Remove gitHub.com certificate.

  3. Execute any operation with git in your terminal. this again ask your username and password.

  1. 搜索证书 gitHub.com。

  2. 删除 gitHub.com 证书。

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

For Windows Users find the key chain by following:

对于 Windows 用户,请通过以下方式找到钥匙链:

Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential

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

回答by Developer Sheldon

I recommend you to do this by simply go to your .gitfolder, then open configfile. In the file paste your user info:

我建议您只需转到.git文件夹,然后打开配置文件即可完成此操作。在文件中粘贴您的用户信息:

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

This should be it.

应该是这样。

回答by Ahmad Vakil

If you have created a new Github account and you want to push commits with your new account instead of your previous account then the .gitconfig must be updated, otherwise you will push with the already owned Github account to the new account.

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

In order to fix this, you have to navigate to your home directory and open the .gitconfig with an editor. The editor can be vim, notepad++ or even notepad.

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

Once you have the .gitconfig open, just modify the "name" with your new Github account username that you want to push with.

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

回答by Pravin

usually the user name resides under git config

通常用户名位于 git config 下

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

although if you still see above doesn't work you could edit .gitconfig under your user directory of mac and update

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

[user]
        name = gitusername
        email = [email protected]