如何使用SSH密钥更改Git提交的作者

4
我已经创建了SSH密钥并将其添加到我的GitLab帐户中。
然后,我进行了一次提交:
git add .
git commit -m "w"
git push origin master

图片描述(https://prnt.sc/q7yc4q)

但是,正如您在提交时看到的我的用户名是root(这是因为我笔记本电脑上的用户是root

  • 1)我如何设置所需的用户名,让我们称之为batman,这样在提交作者中显示的就是batman而不是root?

  • 2)我可以仅使用gitlab界面完成此操作,而无需在我的git配置文件上进行更改吗?(我这样问是因为我有几个git账户,我需要为我的项目使用不同的账户)

2个回答

7
我该如何设置所需的用户名,我们称之为“batman”,使得提交作者显示为batman而不是root?

设置所有项目的用户名/电子邮件

# Set the username for git commit
git config --global user.name "<your name>"

# Set the emailfor git commit
git config --global user.email "<your email address>"

我需要为我的项目使用不同的账户。

为单个项目设置用户名/邮箱

# --local is the default so you don't have to add it

# Set the username for git commit
git config --local user.name="<your name>"

# Set the emailfor git commit
git config --local user.email="<your email address>"

替换提交中错误的作者(将更新所有提交为您的用户)。
git filter-branch -f --env-filter '
    GIT_AUTHOR_NAME="Newname"
    GIT_AUTHOR_EMAIL="newemail"
    GIT_COMMITTER_NAME="Newname"
    GIT_COMMITTER_EMAIL="newemail"
  ' HEAD

替换最新提交中错误的作者

# Update the username and email as explained above
# Re-commit to update the username/email
git commit --amend --no-edit 

3
我收到了一个错误信息:“error: invalid key: user.name=enix426”。通过移除“=”符号,问题得以解决:user.name "<你的名字>"。 - Jimmy

1
在git中,提交作者由选项user.nameuser.email定义。请按照此文档设置它们:https://help.github.com/en/github/using-git/setting-your-username-in-git(这不依赖于使用GitHub或GitLab)。 GitLab/GitHub将使用提交者电子邮件将提交作者链接到GitHub/GitLab帐户。
SSH或HTTP身份验证仅用于检查推送权限,与提交作者无关(不像CVS或SVN这样的集中式VCS)。 因此,这意味着两件事:
  • 您无法在GitLab端更改它
  • 以后更改不容易
为了处理多个提交者名称/电子邮件,最好在克隆存储库级别或“本地”级别上定义设置,方法是使用--local选项与git config
要在以后更改它,您需要重写历史并更改作者,已经有很多相关信息在SO上。

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