254

我在 Git 中进行了一系列提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推送到我的存储库,那么在我这样做之前如何更正这些提交(只有主分支上的 3 个最新提交)?

我一直在看git resetand git commit -C <id> --reset-author,但我认为我没有走在正确的轨道上。

4

8 回答 8

302

警告:现在不推荐使用filter- repo 。

当您触手可及 filter-branch 的功能时,rebase/amend 似乎效率低下:

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

(为了清楚起见,跨行拆分,但不是必需的)

完成后请务必检查结果,以确保您没有更改任何您无意更改的内容!

于 2011-02-13T03:33:19.823 回答
226

与 exec 结合使用时,交互式 rebase 方法非常好。您可以针对特定提交或 rebase 中的所有提交运行任何 shell 命令。

首先设置你的 git 作者设置

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

然后为给定 SHA 之后的所有提交重置作者

git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"

这将弹出您的编辑器以确认更改。您在这里需要做的就是保存并退出,它将通过每次提交并运行 -x 标志中指定的命令。

根据下面@Dave 的评论,您还可以在保持原始时间戳的同时更改作者:

git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD"
于 2014-09-12T19:05:43.550 回答
115

仅更改最后一次提交的作者:

git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit

假设您只想更改最后 N 次提交的作者:

git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit"

笔记

  • --no-edit标志确保git commit --amend不要求额外确认
  • 使用时git rebase -i,您可以手动选择更改作者的提交,

您编辑的文件将如下所示:

pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick dc18f70 bugfix
于 2015-07-28T07:19:43.250 回答
9

GitHub 为此目的记录了此方法(尽管 GitHub 已将其删除)。步骤是:

  1. 打开终端并制作您的仓库的克隆
git clone --bare https://github.com/user/repo.git
cd repo
  1. 编辑以下脚本(替换OLD_EMAILCORRECT_EMAILCORRECT_NAME
#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
  1. 将脚本复制/粘贴到您的终端中,然后按 Enter 键运行它。
  2. 推动你的改变,git push --force --tags origin 'refs/heads/*'你就完成了!
于 2019-02-12T06:42:36.203 回答
9

这里投票最高的答案现在已经过时了。Git 在使用 git filter-branch 时显示这个可怕的警告 -

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites. Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.

要使用git 建议filter-repo的命令-

  1. 使用 pip 安装命令 -

    pip install git-filter-repo
    (需要 git v2.22+ 和 python v3.5+。检查git --version && python3 --version

  2. 修复提交

    • 仅限电子邮件

      git filter-repo --email-callback '
          return email if email != b"incorrect@email" else b"correct@email"
      ' 
      
    • 电子邮件和作者姓名

      git filter-repo --commit-callback '
          if commit.author_email == b"incorrect@email":
              commit.author_email = b"correct@email" 
              commit.author_name = b"Correct Name"
              commit.committer_email = b"correct@email" 
              commit.committer_name = b"Correct Name"
      ' 
      

当您将命令粘贴到终端时,请确保缩进存在。回调使用 python 语法,因此缩进很重要。

在此处阅读有关 filter-repo 回调的更多信息。

于 2021-11-12T19:08:53.317 回答
2

我相信你正在寻找的是git rebase --interactive

它允许您重置到特定的提交,然后抛出历史更改添加或分组提交

在这里你有一个解释https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git-rebase -交互的

于 2011-02-12T23:16:18.583 回答
2

如果你正在寻找一个脚本,这个对我来说很方便。

  1. 从GitHub下载脚本并将其保存到易于访问的位置。

  2. 更改脚本文件的权限以允许其执行:

    chmod +x changeauthor.sh

  3. 使用不正确的提交历史导航到存储库

    cd path/to/repo

  4. 运行脚本(带或不带标志)

    ../path/to/changeauthor.sh --old-email kaka.ruto@example.com \
        --new-email ruto.kaka@example.com --new-name "Kaka Ruto" --remote origin
    

请小心,因为这将重写您当前目录存储库中的所有历史记录!好消息是脚本会为您提供有关您将要执行的操作的警告和信息

在这里阅读更多 https://www.adamdehaven.com/blog/update-commit-history-author-information-for-git-repository/

于 2020-12-01T17:22:52.150 回答
-1

如果你对贬低和修改感到不安全,你可以这样做。同时,您还将设置您可能打算做的全局配置。

git reset HEAD~(撤消最后一次提交)

git config --global user.name "Your Name"

git config --global user.email you@example.com

git commit -m "message"

于 2017-10-13T03:21:37.053 回答