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

我一直在看git reset和git commit -C <id>——reset-author,但我不认为我在正确的轨道上。


我相信你正在寻找的是git rebase -交互式

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

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


警告:现在已弃用,支持过滤器-回购。

当你拥有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

(为了清晰起见,这不是必须的)

当你完成时,一定要检查结果,以确保你没有改变任何你不想改变的东西!


当与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"

只在最后一次提交时更改作者:

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

如果你觉得贬低和修改不安全,你可以这样做。与此同时,你也可以设置全局配置,这可能是你想要做的。

git重置HEAD~(撤销上次提交)

“你的名字”

Git配置——全局用户。电子邮件you@example.com

Git commit -m "message"


这个方法被GitHub记录下来正是为了这个目的(尽管GitHub已经删除了它)。步骤如下:

打开终端,并使您的回购的一个裸克隆

git clone --bare https://github.com/user/repo.git
cd repo

编辑以下脚本(替换OLD_EMAIL、CORRECT_EMAIL和CORRECT_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

复制/粘贴脚本到您的终端,并按enter键运行它。 用git Push——force——tags origin 'refs/heads/*'来推动你的更改,你就完成了!


如果你在找剧本,这个对我来说很有用。

从GitHub下载脚本,并将其保存到易于访问的位置。 修改脚本文件的权限以允许它执行: Chmod +x changeauthor.sh 导航到具有错误提交历史记录的存储库 cd路径/ /回购 运行脚本(带或不带标志) ../path/to/changeauthor.sh——old-email kaka.ruto@example.com \ ——新邮箱ruto.kaka@example.com——新名字“卡卡·鲁托”——远程起源

小心,因为这将重写您当前目录存储库中的所有历史!好的是脚本会给你关于你要做什么的警告和信息

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


投票最多的答案现在已经过时了。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.

Filter-repo还不是git的一部分,需要手动安装。

# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
    
# Install using pip
pip install git-filter-repo    
    

要只替换以前提交中的电子邮件,运行如下命令-

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回调的信息。


正如回答中建议的那样,任务首选git-filter-repo。

然而,要简单地更改作者名称和/或电子邮件,可以使用——mailmap或——use-mailmap来代替回调。

您需要根据格式创建一个mailmap文件(参见git-filter-repo/docs或git-scm.com/docs)

然后简单地运行

git filter-repo --mailmap .mailmap