change a git commit already pushed
git
Solution
By default, remote servers will disallow overwriting already pushed commits. This is because those new commits are different objects which are incompatible to those published before. This means that anyone who has already fetched from the remote since will have major problems fixing it once you overwrite the commit. So you should really reconsider overwriting the commit with something else. Note that `git revert` works with merge commits too, so you might want to consider that instead.
That being said, you still can push that rewritten commit even if it conflicts with what’s on the server. You can do that by force pushing using `git push --force` or `git push -f` in short.
Problem
I accidently overwrote another developper's changes when doing a merge in git. I know how to undo the last commit, that is, my merge. My problem is that I already pushed those commits into our online repository. So if I roll back, do my merge merge again (with his modifications this time) and try to push it again, there will be a conflict (right?). What is right the way to handle this ? EDIT To clarify, here is what the situation looks like : ``` commit A --- commit B --- merge ``` But in the merge I accidentally discarded the modifications made in commit A. This isn't really a problem. I know how to make the changes locally (undo the merge). But my problem is that the whole thing has been pushed into our shared repository (think github or bitbucket).
Related problems
- How do I revert a Git repository to a previous commit?
- How do I undo the most recent local commits in Git?
- How do I delete a commit from a branch?
- How to delete commit that is pushed to the remote repository?
- How do I delete a commit from a branch?
- How do I undo the most recent local commits in Git?
- How to delete commit that is pushed to the remote repository?