Git Interview Question 3: Revert Pushed Commit
Git Interview Questions · Test 1 · question 3 of 10
Yesterday you pushed a commit to the shared main branch that broke the checkout page. Since then, teammates have pushed four more commits on top of it, and several people have already pulled main.
You need to undo the effect of your commit without rewriting main's published history. What should you do?
Show answer and explanation
Answer:
git revert takes an existing commit and records a new commit that reverses the changes it introduced. History only moves forward. Your bad commit and your teammates' four commits all stay where they are, and the new revert commit goes on top. A normal fast-forward git push then publishes it, and everyone who has already pulled can pull again without problems. This is the standard way to undo a commit on a branch other people share.
Option A rewrites published history. Resetting to the parent of your commit also throws away the four commits your teammates pushed after it, and the force push removes them from the remote too.
Option C also moves main back behind the remote tip, so the remote rejects the normal push as a non-fast-forward. --soft keeps the changes in your index, but that doesn't make the push acceptable.
Option D doesn't work. --amend only replaces the most recent commit (HEAD), and your commit is now five commits back. It also needs a force push, which rewrites shared history.
Reference: git-revert