Undo a Pushed Commit With a Revert

Your practice clone now holds a resolved merge, and the store opens at 9 a.m. and closes at 4 p.m. on New Year’s Eve. Conflicts aren’t the only surprise a shared project has in store, though. Sometimes a commit that went to GitHub a while ago turns out to be wrong, and tracking down a wrong result leads you straight to it. By then, GitHub and your laptop have it, and the back office gets it the next time it pulls.

In this lesson, you’ll undo a commit like that with git revert, without changing the history that the other copies already share. You’ll be able to predict what a revert changes and what it leaves alone, and you’ll know which undo command fits which situation.

Find the Commit You Want to Undo

To undo a commit, you first need to know exactly which commit it is and what it changed. For practice, pretend that the deli counter’s new hours were a mistake and have to come off the departments page. The real deli hours stay as they are, because you’ll only undo them in the practice clone, which you never push.

In the practice clone’s terminal, list the latest commits on main. In this lesson, long output lines are wrapped for display:

Language: Shell
$ git log --oneline -7
3245e64 (HEAD -> main) Merge branch 'new-years-eve-open'
775b142 Close at 4 p.m. on New Year's Eve
d356660 (new-years-eve-open) Open at 9 a.m. on New Year's Eve
fbb72b2 (origin/main, origin/HEAD) Merge branch 'main' of
        https://github.com/generic-grocery-dev/generic-grocery
04e9cff Add the deli counter's hours to the departments page
9397289 Add the deals for the week of December 2
93c6d8e Plan the move to GitHub

Commit 04e9cff, which you made on the laptop before the pull, added the deli hours. Your hashes differ, so look for the commit’s message in your own log. It sits below origin/main, which means that GitHub has had it since you pushed the merge, and the back office gets it the next time it pulls. It also isn’t the latest commit, because the merge and both New Year’s Eve commits came after it.

That’s why the undo you know from the first course doesn’t fit here. git commit --amend only replaces your latest commit with a new one. Any change to commits that GitHub already has leaves your history and GitHub’s disagreeing, so Git rejects your next push. Replacing GitHub’s history with your changed one would take a forced push, which overwrites what GitHub has, and this course never does that.

The safe way is to leave the old commit where it is and add a new commit that takes its change back out. Before you undo a commit, read what it did with git show and the commit’s hash:

Language: Shell
$ git show 04e9cff
commit 04e9cff12eb9780e7db5a25e038f8a8d8193fa6b
Author: Martin Breuss <[email protected]>
Date:   Wed Nov 25 09:25:48 2026 -0500

    Add the deli counter's hours to the departments page

    Closes #2

diff --git a/departments.html b/departments.html
index 1bdfbe7..3992e09 100644
--- a/departments.html
+++ b/departments.html
@@ -31,7 +31,8 @@
       </section>
       <section class="department">
         <h2>Deli counter</h2>
-        <p>Sliced meats and cheeses, rotisserie chicken from 11 a.m.,
         and sandwiches made to order.</p>
+        <p>Sliced meats and cheeses, rotisserie chicken from 11 a.m.,
         and sandwiches made to order.
+          The counter is open from 8 a.m. until an hour before the store closes.</p>
       </section>
       <section class="department">
         <h2>Meat and seafood</h2>

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.