Introduction
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 GitHub account - If you don’t have one, you can find the signup instructions here
- Git installed on your computer - You can find the instructions to install Git here.
- Created a GitHub repository
Understanding what a commit is
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:
- Easily roll back small portions of your code if you make a mistake
- Enable you (and others) to understand the evolution of the codebase
Undoing a commit after you push
Let’s recreate a situation where you need to undo a commit after you push.
Setup
- In your repository, create a new file called index.md in our repository.
- Let’s commit and push some changes to our repository. I added a few arbitrary changes for the sake of the tutorial. In the image below, I added the words “hey, there” on line 1 of our index.md. Then I ran the following commands to add, commit, and push the changes.
git add index.md
git commit -m "added a greeting"
git push
Reverting a commit
- But, let’s say I realized I didn’t want to commit or push the words “hey, there” to my repository. Perhaps, I just added that for testing purposes, or I’m missing another line that I wanted to include in the commit. Let’s figure out how to undo the commit:
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 preceded 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
Other options
- A shorter method is to run the command
git revert 0a3d
. Git is smart enough to identify the commit based on the first four (or more) characters. - You don’t have to use the commit hash to identify the commit you want to revert. You can use any value that is considered a gitrevision, including the:
- Tag
- Branch
- Hash
- Reference
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:
- Created the index.md file
- Added a greeting
- Reverted the commit
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!
Top comments (45)
I generally do this
You shoud never force
Why not? The option is there.
Here’s an article on why it’s sometimes dangerous : blog.developer.atlassian.com/force...
Thanks, I'll have a look. But I reacted more to the "never".
Gotcha..I agree..never is a little strong lol. There's exceptions.
Although git is a distributed VCS and the client repositories are more or less independent, it is most common that the one remote repository is treated as the single source of truth.
Force pushing to that remote repo is essentially like saying 'screw what others might have pushed before, my local copy is the new single source of truth now'
The king is dead, long live the king!
You explained that pretty well! Going to refer to your response every time someone questions why I’m not using git push force
Me too. However this article is a new learning..
Nice! Sometimes, I find myself taking that approach as well. I used the one in my article because it's safe and hopefully easy for folks to understand.
If you're working with others, I'd recommend
git push --force-with-lease
to avoid accidentally deleting someones changes.It is considered good practice to disable force pushing. Often it's just banned.
It's considered good practice to have readable commit logs, and force pushing is essential for that task.
Thanks for the
revert
approach instead of the hazardous force push 🙏🏻 some people might use to "cover" their mistakes. It often creates more problem than it solves, whilerevert
is perfectly readable and safe.yeah thank you and I agree. Sometimes there's situations where a force push or "cover up" is needed..maybe for something like environment variables, but I agree that this method is easy to follow.
@blackgirlbytes the problem is who's using it. Every time someone tries to manipulate the history and overwrite stuff, it's a bit risky, by nature. Even if git experts might have appropriate usages of the command, I prefer disabling it as a general rule (e.g., in GitHub, you can protect branches against force push).
If you work alone, you do what you want, including rewriting history, but at your own risks if you damage your repository. If you work in a team, that's not acceptable because you might mess with other's work.
You’re absolutely right
Hey @nenadj ! Happy to have your thoughts here, but I prefer a more positive, kind conversation. I’m not a fan of being dismissive.
Comments like, “Learn everything about git and you won’t have a problem” isn’t helpful to anyone lol. I work at GitHub, and I don’t know everything about Git.
Here are some thoughts on why rewriting history may be dangerous: stackoverflow.com/questions/149100...
:D
Thank you so much :)
hey, this is what i do.
Undo Pushed Commits in Git With Reset and Revert
Undo Pushed Commits With the git reset Command.
Undo Pushed Commits With the git revert Command.
Undo Pushed Commits With the git checkout Command.
Git revert is definitely a nice option. I would love a more in-depth article and look at git rebase and all it's options
More content on git rebase is on the way!
Good stuff!
Congratulations 🎉! That's a helpful article👏
Normally my approach is to a git rebase, but there are some cases where the revert is much more simple 🤩
Thank you! Yeah, that's a common approach..not a wrong approach at all. I found this method is easy for me to explain and for others to understand.
You are welcome! I do agree 💯👏
So to have multiple commits reverted, we have to do git revert multiple times ? If so I assume from the top commit. Any insights on that?
As the article mentioned, you can revert multiple commits by reverting an entire branch or a tag. And no, you don’t need to start from the top. You can pick any commit or group of commits in the repo’s history to revert.
Thanks @eberjoe ! That's correct. @vineetgnair I don't go too deep into it, but you can use any git revision with git revert.
You don’t have to use the commit hash to identify the commit you want to revert. You can use any value that is considered a gitrevision, including the:
Tag
Branch
Hash
Reference
And yes, you don't need to start from the top. I just start from the top in my example.
Love the article! It's very informative
Thank you!!!
Very informative and easy to digest!
Thank you!!!
Completely agree. Force push can get a little tricky for folk, but I can see situations where it's needed.
Excellent article. Recently, during an interview I was asked the difference between
git revert
&git reset
and I did previously soft reset to revert the commit before even pushing the changes to remote repo. This would even work after we had pushed the changes. 🎉🎊🥳Awesome! Glad that it's helpful to you!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.