更改以前提交的git电子邮件

Kev*_*hen 10 git git-commit git-rewrite-history

所以我读了很多关于如何更改以前提交的电子邮件地址但由于某种原因我的更新没有.

我确实喜欢使用我的本地电子邮件(nameofMyComputer@kevin.local)对我的私人仓库进行了40次提交,这很糟糕,因为这封电子邮件与github没有关联(也不可能).

然后我记得我之前需要设置git.config,所以我做了:

 git config user.email "newemail@example.com"
Run Code Online (Sandbox Code Playgroud)

并做了一个测试提交,它完美地工作.

有没有办法可以将我以前的所有提交还原到这封新邮件?

我在SO上阅读了这个问题.在Git中更改了作者和提交者名称以及多次提交的电子邮件并使用了它

 git filter-branch -f --env-filter "                         
                    GIT_AUTHOR_EMAIL='kevin.cohen26@gmail.com'; 
                    GIT_COMMITTER_EMAIL='kevin.cohen26@gmail.com';
                    " 
                HEAD
Run Code Online (Sandbox Code Playgroud)

但它不起作用......我仍然可以看到我之前提交的电子邮件与.patch扩展名为.local电子邮件地址

Chr*_*aes 11

您确实可以像这样一次为许多提交执行此操作:

git rebase -i HEAD~40 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit"
Run Code Online (Sandbox Code Playgroud)

我在这个答案中更好地解决了这个问题.

  • @ChrisMaes:请注意 **`git commit --amend --author ...` 不会更改提交者,只会更改作者!** 因此,虽然可能“看起来”您的电子邮件已更改,但在事实上,存储库中的元数据表明了旧提交者是谁。虽然“filter-branch”(或“filter-repo”)方法更为粗糙,但它们实际上改变了两者。证明:`curl -s https://api.github.com/repos/sshine/author-committer/commits | jq '.[0].commit | { 作者,提交者 }'` -- 我在这里做了一个 `git commit --amend --author="John Doe ..."` ,你可以看到提交者不是 John Doe。 (4认同)
  • 然后`git push --force`? (2认同)
  • 因为这会重写您的提交,是的。考虑使用“git push --force-with-lease”来确保不会破坏同事的工作。 (2认同)

Cod*_*ard 7

正如您在问题中提到的(您找到的答案的链接),这确实是脚本.

注意:

filter-branch正在做一个rebase(将重写分支的历史记录),这意味着每个拥有分支副本的人都必须删除并再次签出.


脚本来源于此处 - Git-Tools-Rewriting-History:

# Loop over all the commits and use the --commit-filter
# to change only the email addresses

git filter-branch --commit-filter '

    # check to see if the committer (email is the desired one)
    if [ "$GIT_COMMITTER_EMAIL" = "<Old Email>" ];
    then
            # Set the new desired name
            GIT_COMMITTER_NAME="<New Name>";
            GIT_AUTHOR_NAME="<New Name>";

            # Set the new desired email
            GIT_COMMITTER_EMAIL="<New Email>";
            GIT_AUTHOR_EMAIL="<New Email>";

            # (re) commit with the updated information
            git commit-tree "$@";
    else
            # No need to update so commit as is
            git commit-tree "$@";
    fi' 
HEAD
Run Code Online (Sandbox Code Playgroud)

脚本做了什么?

它循环遍历所有提交,一旦找到匹配,它就会替换提交者的名称和电子邮件.