AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 12373
Accepted
Chas. Owens
Chas. Owens
Asked: 2009-05-27 12:56:31 +0800 CST2009-05-27 12:56:31 +0800 CST 2009-05-27 12:56:31 +0800 CST

如何编辑 git 的历史记录以更正不正确的电子邮件地址/名称 [关闭]

  • 772
关闭。这个问题是题外话。它目前不接受答案。

此问题似乎与帮助中心定义的范围内的服务器、网络或相关基础架构管理无关。

8年前关闭。

改进这个问题

当我开始使用 git 时,我只是做了 agit init并开始调用addand commit。现在我开始注意了,我可以看到我的提交显示为cowens@localmachine,而不是我想要的地址。看起来好像在设置GIT_AUTHOR_EMAIL并且GIT_COMMITTER_EMAIL会做我想做的事,但我仍然有那些旧的提交带有错误的电子邮件地址/名称。如何更正旧的提交?

git
  • 4 4 个回答
  • 47888 Views

4 个回答

  • Voted
  1. Best Answer
    andy
    2009-05-28T09:51:05+08:002009-05-28T09:51:05+08:00

    您可以通过一次调用 git filter-branch 返回并修复所有提交。这与 rebase 具有相同的效果,但您只需执行一个命令即可修复所有历史记录,而不是单独修复每个提交。

    您可以使用以下命令修复所有错误的电子邮件:

    git filter-branch --env-filter '
        oldname="(old name)"
        oldemail="(old email)"
        newname="(new name)"
        newemail="(new email)"
        [ "$GIT_AUTHOR_EMAIL"="$oldemail" ] && GIT_AUTHOR_EMAIL="$newemail"
        [ "$GIT_COMMITTER_EMAIL"="$oldemail" ] && GIT_COMMITTER_EMAIL="$newemail"
        [ "$GIT_AUTHOR_NAME"="$oldname" ] && GIT_AUTHOR_NAME="$newname"
        [ "$GIT_COMMITTER_NAME"="$oldname" ] && GIT_COMMITTER_NAME="$newname"
        ' HEAD
    

    更多信息可从git 文档获得

    • 85
  2. wu-lee
    2010-07-30T02:54:47+08:002010-07-30T02:54:47+08:00

    Git 的 filter-branch 命令功能强大,但用于任何重要的事情都非常笨拙,例如,如果您有多个作者需要更正。

    这是我发现有用的替代方法,它使用 git-shortlog 手册页中描述的 .mailmap 功能。这提供了一种作者映射机制,我们可以将其与 git log 的格式化工具一起使用。我们可以使用它来生成命令来选择和修改指定的提交序列。

    例如,假设您要更正分支 $BRANCH 的作者身份,从提交 $START 开始。

    您需要在存储库的顶级目录中创建一个 .mailmap 文件,该文件将现有作者姓名映射到正确的作者姓名。您可以通过以下方式获取现有作者姓名的列表:

    git shortlog -se
    

    你需要得到一个像这样的 .mailmap 文件(比如说):

    You <[email protected]>   cowens@localmachine
    You <[email protected]>   root@localmachine
    

    现在您可以使用 git log 的格式化功能生成将 $BRANCH 重写为 $BRANCH2 的命令。

    git checkout -b $BRANCH2 $START
    git log --reverse --pretty=format:"cherry-pick %H; commit --amend --author='%aN <%aE>' -C %H" $START..$BRANCH | sh - 
    

    第一个命令从提交 $START 创建一个新的空分支。对于 $START 和 $BRANCH 结束之间的每个提交,第二个命令 cherry 将原始提交选择到当前分支 $BRANCH2 的末尾,并对其进行修改以正确设置作者。

    这也普遍适用 - 把它放在你的 ~/.gitconfig 中:

    [alias]
        # git reauthor $START..$END
        reauthor = !sh -c 'eval `git log --reverse --topo-order --pretty=format:\"git cherry-pick %H &&  git commit --amend -C %H --author=\\\"%aN <%aE>\\\" && \" $0 ` "echo success" '
    

    因此,当您需要更正作者时,现在您只需生成一个 .mapfile 并执行以下操作:

    git checkout -b $BRANCH2 $START
    git reauthor $START..$BRANCH
    

    原来的分支 ref 可以重新分配给新的,新的被删除:

    git checkout $BRANCH
    git reset --hard $BRANCH2 # be careful with this command
    git branch -d $BRANCH2
    
    • 28
  3. Chas. Owens
    2009-05-28T08:45:54+08:002009-05-28T08:45:54+08:00

    结合How do I fix the metainformation on the first commit in git?

    ### Fix the first commit ###    
    # create a temporary tag for the root-most commit so we can reference it
    git tag root `git rev-list HEAD | tail -1`
    # check it out on its own temporary branch
    git checkout -b new-root root
    # amend the commit
    git commit --amend --author "Foo [email protected]"
    # (or if you've set the proper git **config** values)
    git commit --amend -C HEAD --reset-author
    # now you've changed the commit message, so checkout the original branch again
    git checkout @{-1}
    # and rebase it onto your new root commit
    git rebase --onto new-root root
    ### Fix the rest of the commits ###
    git rebase -i root
    # edit the file to read "edit <commit number> for each entry
    # amend the commit
    git commit --amend --author "Foo [email protected]"
    # (or if you've set the proper git **config** values)
    git commit --amend -C HEAD --reset-author
    # move to the next commit
    git rebase --continue    
    # continue running the last two commands until you see
    # Successfully rebased and updated refs/heads/master.
    ### Clean up ###
    # nuke the temporary branch we created
    git branch -d new-root
    # nuke the temporary tag we created
    git tag -d root
    
    • 9
  4. Chealion
    2009-05-27T14:02:55+08:002009-05-27T14:02:55+08:00

    按照 jedberg 的回答:您可以使用rebase -i并选择编辑有问题的提交。如果你使用git commit --amend --author <AUTHOR DETAILS>然后git rebase continue你可以通过并修复历史。

    • 5

相关问题

  • git如何控制访问权限

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve