How to Undo Pushed Commits with Git

Developer Advocate @ GitHub
Search for a command to run...

Developer Advocate @ GitHub
No comments yet. Be the first to comment.
In this series, I will document technical tutorials for navigating GitHub's awesome features!
I don’t think I’m alone in saying this; early in my career, merge conflicts were the bane of my existence (particularly in 2019). I graduated from a coding boot camp in 2018, and during that time, if I ever experienced a git issue that I didn’t know ...
Raise your hand if you've been personally victimized by the question: 'Will AI replace software engineers?' It's a common debate that drives developers to extremes—either avoiding AI entirely or frantically signing up for every AI course available. ...
I recently went down several rabbit holes about building WYSIWYG editors and popovers. While on maternity leave, I finally have time to deeply explore random technical problems without deadlines - a rare opportunity. Thus far, I've focused on contrib...
This blog post uses storytelling to introduce beginners to Verifiable Credentials, followed by a tutorial on creating them in JavaScript and Kotlin. In Our Last Issue... In our previous issue, we met Dawson Webhart, a junior frontend software enginee...

"Our current credit system says, 'Saddle yourself, so we can give you more to saddle.'" Eric Lapin, President of FormFree, stated during a recent TBD live stream. His comment sheds light on a fundamental paradox of the U.S. credit system: it often re...

Technology adoption doesn't happen with one software engineer coding in solitude. Even though big companies like Meta and Microsoft boast of their humble beginnings in a dorm room or a garage, product adoption requires good marketing. Many software e...

Black Girl Bytes
58 posts
I'm a Developer Advocate at GitHub. I'm passionate about community, code, and education! Reach out to me on Twitter ❤️
One major benefit of version control is that you can roll back your code to any time in history without significantly disrupting your teammates. However, reverting your code isn’t always straightforward, especially when you’re still learning Git or gaining confidence navigating the command line. In this post, I will walk you through undoing a commit after you push your changes via the terminal.
Please note that this post's target audience includes students, early-career developers, or folks who have less experience reverting commits. Please refrain from adding discouraging comments critiquing those less experienced with version control. As software engineers, we have to remember that easy is relative, and there is a steep learning curve in tech. Some people may even have several years of experience, but still feel uncertain about reverting commits.
To follow along, you should already have:
A commit is a snapshot of your repository. Running the command git commit saves a version of the codebase at that point in time. Imagine this: Git takes a “picture” of your codebase with information stating, “This is what your codebase looked like on January 12, 2022 at 3:55 pm. @blackgirlbytes fixed a typo in the codebase at this time.” It’s suggested to make frequent, small commits to:
Let’s recreate a situation where you need to undo a commit after you push.

git add index.md
git commit -m "added a greeting"
git push

Look at the list of commits you made in this repository by running the command:
git log -p

From the image, we can see that we made two commits. The most recent commit indicates that we added the words “hey, there” on line 1. The oldest commit indicates that we created an index.md file. Each commit has a commit hash (the long alphanumeric unique string followed by the word commit), a unique identifier for the attached commit.
To undo the most recent commit, we can copy the commit hash and run the command:
git revert [commit hash]
In my case, I will run
git revert 0a3dbc774ea29bfd68fe55caf1ade33dba1bda35
git revert 0a3d. Git is smart enough to identify the commit based on the first four (or more) characters.You can learn more about git revisions here or on the git scm documentation.
You may see a message similar to the image below. This is called a COMMIT_EDITMSG; it holds a commit message of a commit in progress. It includes information about the commit that you’re reverting, including the author, the branch, the file, and the message. The goal for you is to review it and close the commit editor.
If this file opens in your IDE, you can just close the file at the top. If you’re seeing this message in your terminal via Vim, you can use the command :wq
Learn more about exiting this mode in this Stackoverflow answer.

Now that you’ve exited the terminal, you can finalize the process by running the command git push. After running this command, you’ve successfully reverted your commit.

You can double-check that you reverted the commit by viewing the repo's commit history on GitHub.com. The image below reflects an accurate commit log highlighting the moments I:

Thank you to Nathan for reviewing and providing valuable feedback on this post. ❤️
If this post helped you understand reverting commits, follow GitHub and me on DEV!