简体   繁体   English

推送到远程后,如何用新提交覆盖或替换提交?

[英]How can I overwrite or replace a commit with new one once I have pushed to remote?

So I'm working on a sensitive project with a group and all our sources are on Github.所以我正在和一个小组一起做一个敏感的项目,我们所有的资源都在 Github 上。 I recently pushed a commit and later realized there were lot of mistakes in my push.我最近推送了一个提交,后来意识到我的推送中有很多错误。 I've since fixed all those mistakes on my local copy and am about to push again.我已经在我的本地副本上修复了所有这些错误,并且即将再次推送。 However is there any way I can push and overwrite my last commit?但是有什么方法可以推送和覆盖我的最后一次提交? My reason being, I don't want the others to look up my initial commit and the changes it had...我的理由是,我不希望其他人查看我的初始提交及其所做的更改......

Basically I want to overwrite the old commit with my new one.. so no information about the old commit remains for other group members to see.基本上我想用我的新提交覆盖旧提交。所以没有关于旧提交的信息可供其他组成员查看。

Usually, once something is out on Github (or a public repo), there's no way to make sure no one else has the bad commit.通常,一旦在 Github(或公共仓库)上发布了某些内容,就无法确保没有其他人有错误的提交。

If you simply want cleaner code history though, the way to do what you ask requires you to overwrite history both locally and on the remote project.但是,如果您只是想要更清晰的代码历史记录,那么执行您所要求的操作的方式需要您在本地和远程项目上覆盖历史记录。

What you want is to either do a git commit --amend to modify your old commit if you've not already created a fresh commit with your changes.如果您尚未使用更改创建新提交,您想要的是执行git commit --amend来修改旧提交。

If you've already created a fresh commit, you'll want to use git rebase -i to squash your commit on top of the old one.如果您已经创建了一个新的提交,您将需要使用git rebase -i将您的提交压缩到旧提交之上。

After you've made this change locally, and verified your commit looks the way you want it to, you'll have to git push --force to overwrite history on the Github remote.在您在本地进行此更改并验证您的提交看起来像您想要的方式之后,您必须使用git push --force来覆盖 Github 远程上的历史记录。

Here's a tutorial on how to re-write history in git, it explains both the approaches listed in this answer .这是一个关于如何在 git 中重写历史的教程,它解释了这个答案中列出的两种方法。

I would not advise you to do this as GIT isn't meant to remove and/or edit commits.我不建议您这样做,因为 GIT 并不意味着删除和/或编辑提交。 You could just revert your previous commit and then commit the "valid" code again.您可以只恢复之前的提交,然后再次提交“有效”代码。

Or you can Rebase your branch.或者你可以重新设置你的分支。 Or take a look at this Answer .或者看看这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM