Interview Q&A / Git & GitHub

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do you undo a commit that you already pushed to a shared branch?

SHORT ANSWER

Use git revert, which adds a new commit that cancels the bad one. History is not rewritten, so teammates can pull normally. Avoid reset plus force-push on shared branches.

Explain it simply

Once a bad commit is on the shared branch, others may already have it. Erasing it would be like tearing a page from a notebook everyone photocopied. A revert is writing a correction on the next page.

git revert <hash> builds a new commit containing the opposite changes. History remains truthful and a normal push is enough. To undo a merge commit use -m 1 to say which parent is the mainline. If you revert a faulty merge and later want the branch back, you must revert the revert first.

Example

bash
git revert 7f3a9d2
git push origin main
# reverting a merge commit
git revert -m 1 c91e0b4

What to say in the interview

"On a shared branch I use git revert, which creates a new commit undoing the change without rewriting history. Reset with force-push would disturb everyone who already pulled. For merge commits I add -m 1 to choose the mainline parent."

#git#revert#undo

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.