如何用 git 上的新提交覆盖提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35543574/
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
how to overwrite commit with new one on git?
提问by sparta93
So I'm working on a sensitive project with a group and all our sources are on Github. I recently pushed a commit and later realised 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...
所以我正在和一个小组一起做一个敏感的项目,我们所有的资源都在 Github 上。我最近推送了一个提交,后来意识到我的推送有很多错误。我已经在我的本地副本上修复了所有这些错误,并准备再次推送。但是有什么方法可以推送和覆盖我的最后一次提交?我的原因是,我不希望其他人查找我的初始提交及其所做的更改...
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.
基本上我想用我的新提交覆盖旧提交..所以没有关于旧提交的信息可供其他组成员查看。
Any help will be appreciated! Thank you.
任何帮助将不胜感激!谢谢你。
NOTE: Just noticed that this question was marked as a duplicate. To clarify, my question is about overwriting a commit that has already been pushed. My question is NOT about changing an incorrect commit message.
注意:刚刚注意到这个问题被标记为重复。澄清一下,我的问题是关于覆盖已经推送的提交。我的问题不是关于更改不正确的提交消息。
回答by ffledgling
Usually, once something is out on Github (or a public repo), there's no way to make sureno 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 中重写历史记录的教程,它解释了此答案中列出的两种方法。