【问题标题】:How to change author of git commit using ssh keys如何使用 ssh 密钥更改 git commit 的作者
【发布时间】:2020-04-01 22:45:20
【问题描述】:

我已经创建了 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 帐户,我需要使用 我的项目的不同帐户)

【问题讨论】:

    标签: git gitlab git-commit git-filter-branch


    【解决方案1】:

    如何设置所需的用户名让我们称之为 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 
    

    【讨论】:

    • 我收到错误“错误:无效密钥:user.name=enix426”。通过删除“=”符号解决:user.name“
    【解决方案2】:

    在 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 上已经有很多相关信息。

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 1970-01-01
      • 2021-06-06
      • 2023-01-16
      • 2012-02-24
      • 1970-01-01
      • 2023-01-05
      • 2016-08-15
      • 2014-01-18
      相关资源
      最近更新 更多