git 修改 Github 上推送提交的用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3593722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:32:19  来源:igfitidea点击:

Amend username for a pushed commit on Github

gitgithub

提问by Brock Woolf

I made a push to a newly forked git repo on Githubbut after committing i noticed that my username was incorrect. The username I pushed was "Brock Woolf" but it should have been brockwoolfwhich is my username on github.

我在Github上推送了一个新分叉的 git repo,但在提交后我注意到我的用户名不正确。我推送的用户名是“Brock Woolf”,但它应该brockwoolf是我在 github 上的用户名。

I already changed the default locally like this:

我已经像这样在本地更改了默认值:

git config --global user.name "brockwoolf"

But how can I change the username on the already pushed change?

但是如何更改已推送更改的用户名?

回答by Cascabel

The already pushed change, if people have pulled it, is something you'll have to live with. If no one's pulled it (i.e. you realize your mistake right after pushing), you can amend your commit:

已经推动的变化,如果人们已经拉动它,你将不得不接受它。如果没有人拉它(即您在推后立即意识到自己的错误),您可以修改您的提交:

git commit --amend

Make sure you don't add any new changes to the commit - don't use -a, don't use git addfirst. Then you can force the push, since this is a non-fast-forward change:

确保您没有向提交添加任何新更改 - 不要使用-a,不要git add先使用。然后你可以强制推送,因为这是一个非快进的变化:

git push -f

If anyone's already pulled the commit with the incorrect name... this probably won't actually mess them up, since merging it with something containing the original commit should be easy; the patches are the same. However, if that person ever pushed back to your repo, they'd push that merge - along with the original commit on one side of it. Kind of defeats the purpose of renaming yourself if you end up with bothnames in the repo. (This is exactly the problem I described in my comment on the OP's answer.)

如果有人已经使用错误的名称拉取了提交......这实际上可能不会把他们搞砸,因为将它与包含原始提交的内容合并应该很容易;补丁是一样的。然而,如果那个人推回你的仓库,他们会推那个合并——连同它一侧的原始提交。如果最终在 repo 中使用了两个名字,那么重命名自己的目的就有点失败了。(这正是我在对 OP 答案的评论中描述的问题。)

回答by stevenspiel

As noted here, you can do

正如这里所指出的,你可以做

git commit --amend --author="Author Name <[email protected]>"
git push -f

回答by Brock Woolf

Sweet I figured it out:

亲爱的,我想通了:

git commit -a --amend
git pull
git push

Feel free to answer, if you have a better way I'll mark yours correct.

随意回答,如果你有更好的方法,我会标记你的正确。