如何更改Git的默认用户名

4
当我尝试使用“git push origin master”推送代码时,它要求我输入“https://test_ankit@bitbucket.org”的密码,但我的实际用户名是“rest_ankit”。
我使用了这个命令来更改它:“git config --global user.name“rest_ankit””,但它没有起作用。它仍然要求错误用户名的密码,并显示身份验证失败。
请帮帮我。谢谢。
2个回答

4
您可以使用以下方式更改原始URL:
git remote set-url https://rest_ankit@bitbucket.org/username/reponame.git

那将同时更改与URL相关联的用户名。
使用完整的存储库URL,而不仅仅是bitbucket.org,而是bitbucket.org/username/reponame.git,将usernamereponame替换为正确的值。
请注意,http凭证与git config user.name无关:后者用于提交作者身份,而不是在推送/拉取期间进行身份验证。
Git 2.25.1(2020年2月)澄清了有关提交者/作者身份的文档。

查看 提交 69e104d, 提交 813f602, 提交bc94e58(2020年1月22日),由布莱恩·M·卡尔森(bk2204完成。
(由Junio C Hamano -- gitster --提交c9ccf9d中合并,2020年1月30日)
在此处讨论

doc:提供用户名称格式指导

署名:brian m. carlson

有一种常见误解,即user.name变量在某种程度上控制身份验证,因此,新手们经常尝试更改它以解决身份验证问题。
文档规定,该变量代表某种形式的人类个人姓名,尽管这并不是必需的。

此外,解决Unicode是否受支持的问题。

使用术语“个人姓名”,因为这很可能引起预期对比,适用于可能具有不同命名约定的文化,并且易于理解非英语为母语的人。

表明按惯例使用“某种形式”,因为人们可能使用昵称或首选名称而不是全名。

将可能对身份验证感到困惑的用户指向适当的配置选项。

在配置选项描述中提供此信息的缩写形式。

因此,git config user man page 解释了:

name 形式的这些变量通常指代某种个人名称。

git commit 包括:

COMMIT INFORMATION

Author and committer information is taken from the following environment variables, if set:

GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE

(nb "<", ">" and "\n"s are stripped)

The author and committer names are by convention some form of a personal name (that is, the name by which other humans refer to you), although Git does not enforce or require any particular form.
Arbitrary Unicode may be used, subject to the constraints listed above.
This name has no effect on authentication; for that, see the credential.username variable, used in gitcredentials.


致命错误:未找到 https://rest_ankit@bitbucket.org/info/refs:您是否在服务器上运行了 git update-server-info? - Ankit
@Ankit,你需要使用完整的仓库URL:https://rest_ankit@bitbucket.org/username/reponame - VonC
这个能否在不改变源URL的情况下通过密钥或类似方式实现呢? 我有一个包含50个子模块的git,由于团队中有很多工作者,无法更改每个子模块的URL以匹配我的用户名。 - Micha
@Micha 是的:在父存储库中:git config --global url."https://myusername@github".insteadOf https://github - VonC
@VonC 很酷!这对我的本地设置是一个改进,也许我甚至可以通过切换到 git@ 语法来摆脱密码提示。使用相对 URL 可以解决子模块的问题。 - Micha

4

ssh

git remote set-url origin git@bitbucket.org:{new team or account name}/{repository name}.git

https

git remote set-url origin https://{username}@bitbucket.org/{new team or account name}/{repository name}.git

然后使用检查更改。
git remote -v

如果您在推送时遇到问题,并且推送的远程未更改,则输入以下内容:
对于ssh:
git remote set-url --push origin git@bitbucket.org:{new team or account name}/{repository name}.git

关于 https

git remote set-url --push origin https://{username}@bitbucket.org/{new team or account name}/{repository name}.git

这对我有效。被接受的答案缺少 origin - kaybee99

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