how to overwrite commit with new one on git?

This question already has an answer here:

  • How to modify existing, unpushed commits? 27 answers

  • 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.

    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.

    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.

    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.

    Here's a tutorial on how to re-write history in git, it explains both the approaches listed in this answer.


    I would not advice you to do this as GIT isn't meant to remove and/or edit commits. 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.

    链接地址: http://www.djcxy.com/p/1396.html

    上一篇: git中的强制推送和正常推送有什么不同?

    下一篇: 如何覆盖提交新的git?