在特定提交上更改作者/邮件 [英] Changing author/mail on specific commit/s

查看:219
本文介绍了在特定提交上更改作者/邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bitbucket上有一个与在线存储库(来源)对齐的存储库,同时使用Atlassian Sourcetree作为管理它的工具.

I have a repository on bitbucket which is aligned with the online repository (origin), while using Atlassian Sourcetree as tool for managing it.

假设它有20次提交.

由于某些原因,Sourcetree共享更改项目设置的乐趣,因此我错误地推送了2次commit(比方说使用其他机器的其他同事,commit 15和16).

Sourcetree for some reason haves its share of fun changing project settings and I wrongly pushed 2 commits (let's say commit 15 and 16 as some other coworker which uses the machine too.

我想在这些提交中输入我的名字和邮件.

I want to put my name and mail in those commits.

让我们想象这样的树(没有树枝,只有主人)

Let's imagine the tree like this (without branches, just master)

提交编号->评论->提交ID->作者/邮件

Commit number -> comment -> commit id -> author/mail

  • 18->提交评论blabla-> 29huh23->我me@mycompany.com
  • 17->提交17功能XYZ-> abs2881->我me@mycompany.com
  • 16->提交16个功能KWZ-> anu2716->某人other@othercompany.com
  • 15->提交15个功能IHZ-> 11suhs2->某人other@othercompany.com
  • 14->提交14个功能UYZ-> 1uuhw87->我me@mycompany.com

other@othercompany.com的其他人应该成为我.

someone other@othercompany.com should become me.

我是该仓库的管理员.

如何以简单的方式做到这一点?有可能来自Sourcetree吗?我应该在终端上这样做吗?

How to do it in a simple way? It's possible from Sourcetree? Should I do it from the terminal?

手动修改.git文件夹中的文件并创建一个新的仓库并在那里提交是一个可行的解决方案吗?

Could it be a workable solution to manually modify files in the .git folder and create a new repo and commit there?

问题不是重复的,因为建议重复的答案不能很好地发挥作用.

Question is not duplicate as suggeste duplicate answers does not work well.

编辑-任何人在不进行过多研究的情况下如何正确查找的正确程序

由于每个人都将其标记为重复,而并非每个人都将其标记为重复,因为其他过程不完整,并让对git不太友善的人(如我)遇到问题,因此正确的过程是:

Since everyone is marking this as duplicate while is not, as other procedures are INCOMPLETE and leave someone (like me) who is not very cool with git with problems and questions, the correct procedure is:

从SourceTree中,在打开存储库后,启动带有终端按钮在右上方的终端.

From SourceTree, launch terminal with the terminal button on top right when the repo is open.

然后粘贴并执行,并使用适合您情况的替换

Then paste this and execute, with the substitutions suitable for your case

    #!/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

但是,这将产生一个辅助的备份"分支,因为在重写历史记录时,这使我充满了疑虑.

This though, will produce a secondary "backup" branch, because when rewriting history, which left me with much doubts.

我必须执行此附加步骤才能删除备份"分支,并实际上将新历史记录推送到存储库中,并使用正确的名称/邮件查找新历史记录:

I had to do this additional step to remove the "backup" branch and actually push the new history to the repo and find the new history with correct name/mail:

git push --force --tags origin 'refs/heads/*'

此处完整指南.

感谢所有为我指出正确方向的人.

Thanks to everyone who pointed me to the right direction.

推荐答案

没有简单"的方法可以做到这一点.作者和提交者信息是提交的一部分;更改它们将更改提交ID.更改提交ID意味着必须更改后续提交的父信息,并且更改那些提交的ID.换句话说,要更改它,您需要重写历史记录.

There is not a "simple" way to do this. The author and committer information are part of the commit; changing them changes the commit ID. Changing the commit ID means the subsequent commits' parent info has to change, and that changes those commit's IDs. In other words, to change it you need a history rewrite.

历史记录重写并不是特别困难,但是当受影响的提交已经被共享时(或者更确切地说,当将已被删除的受影响的提交的旧"版本删除时),历史记录重写会付出巨大的代价.共享的分支历史).

History rewrites are not particularly difficult to do, but they come with a significant cost when the affected commits have already been shared (or, maybe more precisely, when the "old" version of an affected commit would be removed from the already-shared history of a branch).

要进行这样的重写,必须与已经收到旧"提交的其他所有人进行协调.我经常听到人们说这太麻烦了,因为有多少人拥有回购协议.在这种情况下,您无力进行重写.如果您忽略该建议,那么可能会发生其他人,试图修复"他们收到的错误,将撤消您的重写,然后您将拥有一个真正分散的存储库,对任何人来说都没有多大用处,直到更大的协作已完成清理工作.

To do such a rewrite, it is necessary to coordinate with everyone else who has already received the "old" commits. I often hear people say this would be too burdensome, because of how many people have a repo. In that case, you can't afford to do the rewrite. If you ignore that advice, what will probably happen is someone else, trying to "fix" the error they receive, will undo your rewrite, and then you'll have a truly splintered repository that's of little use to anyone until an even bigger coordinated effort is completed to clean it up.

话虽如此,如果您 still 只想清理一些提交的电子邮件地址而进行历史记录重写,请使用--env-filter选项签出git filter-branch.

With all that said, if you still want to do a history rewrite just to clean up a few commits' email addresses, check out git filter-branch with the --env-filter option.

这篇关于在特定提交上更改作者/邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆