如何使用 ssh 密钥更改 git 提交的作者

How to change author of git commit using ssh keys

我已经创建了 ssh 密钥并将其添加到我的 git实验室帐户。

然后,我做了一个提交:

git add .
git commit -m "w"
git push origin master

(https://prnt.sc/q7yc4q)

但是你可以看到我提交时的用户名是 root(这是因为我笔记本电脑上的用户是 root

How do I set the desired username let's call it batman, so instead of root I want batman to be displayed as commit author?

为所有项目设置 username/email

# 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>"

... I need to use different accounts for my projects

为单个项目设置 username/email

# --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 

在 git 中,提交创作由选项 user.nameuser.email 定义。请按照此文档设置它们:https://help.github.com/en/github/using-git/setting-your-username-in-git(它不依赖于使用 GitHub 或 GitLab)。 GitLab/GitHub 将使用提交者电子邮件 link 将作者提交到 GitHub/GitLab 帐户。

SSH 或 HTTP 身份验证仅用于检查推送权限,与提交创作无关(与 CVS 或 SVN 等集中式 VCS 不同)。 所以你这意味着两件事:

  • 你不能在 GitLab 端更改它
  • 以后不容易改

要处理多个提交者 names/emails,最好是在克隆存储库级别或 "local" 上定义设置,方法是使用 --local 选项和 git config

以后要改的话,需要在改作者的同时重写历史,SO上已经有很多这方面的资料了。