Git push keeps writing commit with wrong username

百般思念 提交于 2020-08-10 06:24:04

问题


I've created a new project on a shared machine and the commits in my repositories are under the wrong username on Git. I've changed the credentials in the credentials manager in windows, I've followed this guide but the author has not changed, even if when I push it asks me username and password for pushing, and I insert mine. Still, on the repository the author of the commit is not me. I've also tried this solution but still doesn't work.

What else can I try?

UPDATE

Steps:
- git commit -m "message"
- git push origin master

Then it asks for email and password. I insert mine, it pushes correctly and then I go to the repository and see this:

After git log I see this:

It's all mixed up.

In the repository the name is not correct.


回答1:


You're laboring under a false premise, so you cannot fix this until you change your approach: git push transfers existing commits from one repository to another. The transfer process uses your credentials—the ones from the manager for https://, or the stored ssh ones for ssh://—but the commits are already frozen: they have whatever name is set for their author and committer, and these cannot be changed.

Therefore, you need to make new and different commits. When you make commits, the name and email address that Git uses here are not data you set in any credentials, nor stored in any ssh keys. Instead, these use only your user.name and user.email settings.

If you have existing commits that you like, you can copy them to new-and-improved commits that are almost exactly the same as the originals, but slightly different in that they:

  • have a different author and committer name (your new and improved, corrected ones), and

  • have different hash IDs (because they're different commits).

You can then tell your Git: Throw out those icky old commits and use my shiny new improved ones instead. You'll stop seeing the old commits and will see only the new ones. If you look closely, you will see that the new ones have new hash IDs, even though everything else about them is identical.

To do that, see How to change the author and committer name and e-mail of multiple commits in Git?. Note that you should be careful to copy only the commits you made using the wrong name-and-email-address setting. The accepted answer at the linked question copies any commits that match one particular email address, so if you mistakenly used, say, Linus Torvalds' email address and try this on a Linux system, you'll be replacing all of his commits. But in the typical case, where your wrong email was unique, you'll replace just the last few commits that you got wrong.

(Note: it may help if, in your question, you show exactly what steps you are taking and the exact results. For instance, if the git push is failing with non-fast-forward, we'll know more about what's going wrong.)




回答2:


For new commits set a new Git Name, it will automatically overwrite the old Name and Email ID in the config file. Also, the other answer by Torek points out how to replace the previous bad commits and replace with newer ones having the correct Name and Email IDs. For newer commits I would also recommend signing the commits using GPG keys thereby ensuring that you are the one making those commits.



来源:https://stackoverflow.com/questions/54647396/git-push-keeps-writing-commit-with-wrong-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!