Check out a free preview of the full Everything You'll Need to Know About Git course

The "Revert" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen explains the concept of reverting changes in Git. He demonstrates how to revert a commit using the "git revert" command and shows how conflicts can arise during the process. ThePrimeagen also highlights the importance of using squashed rebase to simplify the process of reverting changes.

Preview
Close

Transcript from the "Revert" Lesson

[00:00:00]
>> All right, so sometimes we have a decision we have to face as programmers, which is to push forward or to roll back. I think we've all been there, sometimes you got to revert, right? You made a change, it was a failure, nobody's happy about it, nobody likes you, and now you need to revert it.

[00:00:18]
So by the way, if you're confused about the difference between revert and restore, just go read about restore. Restore effectively just changes any change into what the previous state was, whereas revert is effectively like the anti matter to matter. It is gonna make an inverted change as a commit, and then you commit that.

[00:00:41]
So it actually changes what you've done invertedly, we'll see in just a moment. It's very fantastic, I'm not sure how else to describe it, other than it just, it did what you did but backwards, right? All right, so let's look at it. So reverting is simple, you just need to provide a commit, right, I just need to revert something and then it will work.

[00:01:01]
All right, so let's go to hello git, and let's revert the commit with log message E. Before removing it to check the contents of the commit via log with -p. So, does anyone remember how to search the friendly logs? Dash dash grep, that's right, great job everybody, I can't believe you guys all said that out loud.

[00:01:24]
So I wanna go to hello git and I'm gonna go get log --grep E and then we'll do a little -p to print out the contents. So look at this, right here, we made this earlier today, we have a little plus e right here, absolutely wonderful. And our commit message was of course an E as well.

[00:01:42]
Awesome, everyone's happy, I'm happy, you're happy, Dustin is kinda frowning, but most of us are happy here. All right, so now we need to revert this. So I'm gonna actually take this guy right here, the commit char, and I want to be able to take it, and I'm gonna use it for reverting.

[00:01:57]
So I'm gonna copy the commit sha, and I'm gonna make sure that that's what we're supposed to do at this point, which we're not supposed to do this quite yet. So we're gonna go through all this one. Fantastic, you can manually look through it. Now let's revert it.

[00:02:07]
So I'm gonna go git revert and then the hash that you have. So git revert hash. Conflicts Is just like I hate them and I can never seem to get away from them, I don't know what is wrong with my life, but anyways, here we go, we've conflicted.

[00:02:30]
No, preplanned, [LAUGH] you can get conflicts in reverting, right? Not only are you getting kicked in the shin for the fact but you just made a mess up, you get sometimes conflicted as well, and it's just like, this is the worst possible scenario of all time. All right, it's emotionally bruising, so we got to do something about this.

[00:02:50]
So what do we do? Well, we go again, get status, again, modified both, this looks very, very normal. You'll also notice up here, we do a git revert skip or abort, so lets go to README, and this one's a little bit more confusing, right? Yeah, this is our change, and okay, this is what we got to apparently.

[00:03:09]
It somehow has both EN downstream in here, it probably shouldn't have both of them. So what do we get to do here? Well, let's delete that, right, delete the conflicts, and then I only want to delete E, right,? Cuz that's what I was trying to remove, was E, all right?

[00:03:25]
So I'll save it, and then if we go git status, git add, git status, you'll see right here that we need to now go git revert continue. Awesome, so now it gives us a beautiful commit message, revert E, this reverts this specific commit. So that we have this nice log, you can see that this action was intentional.

[00:03:48]
By the way, this is the greatest argument for why you want to rebase squash, because there's no merge commits. All this history that you've just created and now we need to remove this change, that sucks. Whereas if you have a nice squashed rebase no merge can history, removing the change is very, very simple.

[00:04:03]
There's just one step, which is just the change you want to revert. I'm gonna take this, I'm gonna save it, and there we go. Git status, and you'll see we're on a clean thing, git log oneline, you'll see right here, revert E, I say revert, is that even correct?

[00:04:17]
Is it revert or is it revert if you're talking about it in length and that kind of sense, I don't even know, it's a great question to me. You can see right here there's E, we've reverted it, the revert is literally the inverse commit, as you can see.

[00:04:29]
It's not that it removes it from our history, because if it removed it from our history, what would happen? Every branch would now have invalid state and everything would be horrible. So a revert is when you actually put the antimatter at the front, is how I like to think of it, you just put the opposite commit in front and merge it in.

[00:04:48]
So there we go, we got that opposite commit going, absolutely fantastic. All right, that's it, little quick one on revert, you rarely need to use it, that's about it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now