Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

git - 如何在Git中更改多次提交的作者和提交者名称以及电子邮件?(How to change the author and committer name and e-mail of multiple commits in Git?)

I was writing a simple script in the school computer, and committing the changes to Git (in a repo that was in my pendrive, cloned from my computer at home).

(我当时正在学校计算机上编写一个简单的脚本,然后将更改提交到Git(在笔驱动器中的一个回购中,该回购是从家里的计算机中克隆的)。)

After several commits I realized I was committing stuff as the root user.

(经过几次提交后,我意识到我正在以root用户身份提交东西。)

Is there any way to change the author of these commits to my name?

(有什么办法可以将这些提交的作者更改为我的名字?)

  ask by Flávio Amieiro translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

NOTE: This answer changes SHA1s, so take care on using it on a branch that has already been pushed.

(注意:此答案会更改SHA1,因此请小心在已推送的分支上使用它。)

If you only want to fix the spelling of a name or update an old email, git lets you do this without rewriting history using .mailmap .

(如果您只想修正名称的拼写或更新旧电子邮件,则git允许您执行此操作而无需使用.mailmap重写历史记录。)

See my other answer .

(看到我的其他答案)

Using Interactive Rebase (使用交互式基础)

You could do

(你可以做)

git rebase -i -p <some HEAD before all of your bad commits>

Then mark all of your bad commits as "edit" in the rebase file.

(然后,将所有错误提交标记为rebase文件中的“ edit”。)

If you also want to change your first commit, you have to manually add it as first line in the rebase file (follow the format of the other lines).

(如果您还想更改您的第一次提交,则必须手动将其作为重新提交添加到rebase文件的第一行中(遵循其他行的格式)。)

Then, when git asks you to amend each commit, do

(然后,当git要求您修改每次提交时,请执行)

 git commit --amend --author "New Author Name <[email protected]>" 

edit or just close the editor that opens, and then do

(编辑或仅关闭打开的编辑器,然后执行)

git rebase --continue

to continue the rebase.

(继续进行基地调整。)

You could skip opening the editor altogether here by appending --no-edit so that the command will be:

(您可以通过添加--no-edit来跳过此处完全打开编辑器的操作,这样命令将是:)

git commit --amend --author "New Author Name <[email protected]>" --no-edit && 
git rebase --continue

Single Commit (单次提交)

As some of the commenters have noted, if you just want to change the most recent commit, the rebase command is not necessary.

(正如一些评论者指出的那样,如果您只想更改最近的提交,则不需要rebase命令。)

Just do

(做就是了)

 git commit --amend --author "New Author Name <[email protected]>"

This will change the author to the name specified, but the committer will be set to your configured user in git config user.name and git config user.email .

(这会将作者更改为指定的名称,但是将在git config user.namegit config user.email中将提交者设置为您配置的用户。)

If you want to set the committer to something you specify, this will set both the author and the committer:

(如果要将提交者设置为指定的内容,则会同时设置作者和提交者:)

 git -c user.name="New Author Name" -c [email protected] commit --amend --reset-author

Note on Merge Commits (关于合并提交的注意事项)

There was a slight flaw in my original response.

(我的原始回复有轻微缺陷。)

If there are any merge commits between the current HEAD and your <some HEAD before all your bad commits> , then git rebase will flatten them (and by the way, if you use GitHub pull requests, there are going to be a ton of merge commits in your history).

(如果当前HEAD和您的<some HEAD before all your bad commits>之间有任何合并提交,然后<some HEAD before all your bad commits> ,那么git rebase将使它们git rebase平(顺便说一句,如果您使用GitHub pull请求,将会有大量的合并提交您的历史记录)。)

This can very often lead to very different history (as duplicate changes may be "rebased out"), and in the worst case, it can lead to git rebase asking you to resolve difficult merge conflicts (which were likely already resolved in the merge commits).

(这通常会导致非常不同的历史记录(因为重复的更改可能会被“重新基准化”),在最坏的情况下,它可能导致git rebase要求您解决困难的合并冲突(在合并提交中可能已经解决了) )。)

The solution is to use the -p flag to git rebase , which will preserve the merge structure of your history.

(解决方案是使用-p标志来git rebase ,这将保留历史记录的合并结构。)

The manpage for git rebase warns that using -p and -i can lead to issues, but in the BUGS section it says "Editing commits and rewording their commit messages should work fine."

(git rebase的联机帮助页警告说,使用-p-i可能会导致问题,但是在BUGS部分中,它说“编辑提交并改写其提交消息应该可以正常工作。”)

I've added -p to the above command.

(我在上面的命令中添加了-p 。)

For the case where you're just changing the most recent commit, this is not an issue.

(对于仅更改最新提交的情况,这不是问题。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...