Git 更改未推送提交的作者
Git change author of not pushhed commits
我有两次提交,(已经提交了错误的电子邮件)所以我的推送被拒绝了。
如何在不丢失更改的情况下更改这些提交的电子邮件?
已有此答案
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. 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). Then, when git asks you to amend each commit, do
git commit --amend --author "New Author Name <email@address.com>"
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:
git commit --amend --author "New Author Name <email@address.com>" --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. Just do
git commit --amend --author "New Author Name <email@address.com>"
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
. 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 user.email=email@address.com 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). 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). The solution is to use the -p
flag to git rebase
, which will preserve the merge structure of your history. 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."
I've added -p
to the above command. For the case where you're just changing the most recent commit, this is not an issue.
在此 post 中:Change the author and committer name and e-mail of multiple commits in Git
我有两次提交,(已经提交了错误的电子邮件)所以我的推送被拒绝了。 如何在不丢失更改的情况下更改这些提交的电子邮件?
已有此答案
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. 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). Then, when git asks you to amend each commit, do
git commit --amend --author "New Author Name <email@address.com>"
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:git commit --amend --author "New Author Name <email@address.com>" --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. Just do
git commit --amend --author "New Author Name <email@address.com>"
This will change the author to the name specified, but the committer will be set to your configured user in
git config user.name
andgit 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 user.email=email@address.com 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>
, thengit 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). This can very often lead to very different history (as duplicate changes may be "rebased out"), and in the worst case, it can lead togit rebase
asking you to resolve difficult merge conflicts (which were likely already resolved in the merge commits). The solution is to use the-p
flag togit rebase
, which will preserve the merge structure of your history. The manpage forgit rebase
warns that using-p
and-i
can lead to issues, but in theBUGS
section it says "Editing commits and rewording their commit messages should work fine."I've added
-p
to the above command. For the case where you're just changing the most recent commit, this is not an issue.
在此 post 中:Change the author and committer name and e-mail of multiple commits in Git