Reverting changes made in older commits
Okay, so we've learned how to check the history of our repository and how to compare changes between commits.
What if we found a bug in a commit that was made a while ago? How can we undo the changes made by that commit?
The simplest way to undo changes is to simply replicate the reverse changes and commit them. But what if the changes are complex, and you don't want to mess with them?
In Git, you can revert any previous commit using the git revert command followed by the commit hash you want to revert. Note that this command does not delete the commit; it just adds a new commit that includes reverse changes. Adding an extra commit may seem redundant, but it helps to keep the history consistent. After all, what if you realize that the changes you reverted were actually correct?
Let's practice this. First, let's introduce a "bug" into our code.
<html>
<head>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a bug!</p>
</body>
</html>
Add the line <p>This is a bug!</p>
to hello.html
, stage and commit the changes with the commit message Introduced a bug
.
Great! At this point, we can still cancel the changes with git commit --amend
. But what if we discover the mistake too late? Let's make another okay-ish commit, and then show how to revert the one we did before that contained undesired changes.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a bug!</p>
</body>
</html>
Add the line <!DOCTYPE html>
to hello.html
, stage and commit the changes with the message Added HTML5 doctype
.
Now, let's check the commit hash of the commit that introduced the bug. Run git log
to find out.
Run git log
and find the commit hash of the commit that introduced the bug.
Tip: Remember that you can scroll through the output of git log
using the and arrow keys on your keyboard. To exit pager mode, press q.
With the commit hash, we can now revert the changes made by that commit. Replace
with the actual hash in the following command:<commit>
git revert <commit>
This will open your default text editor asking for a commit message. You can leave the default message, save, and close the file.
Revert the commit that introduced the bug using git revert
.
Great, the bug has been reverted. Now check the difference made by the last commit. Remember how to do that with git diff? You'll need to remember how to reference one commit before the latest commit.
Check the diff between the last two commits.
As you see, this is the exact reverse of the changes made by the commit that introduced the bug. This is how you can revert any commit in your repository.
Be aware that reverting things this way is not error-proof and may introduce conflicts if Git is unable to apply the reverse changes. In that case, you'll need to resolve the conflicts manually. If you'd like to learn more about resolving conflicts, stick around until Part II of the course.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.