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

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


当前回答

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

从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 rebase -交互式

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

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

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

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/*'来推动你的更改,你就完成了!

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

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

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

然后简单地运行

git filter-repo --mailmap .mailmap

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

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