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

The "Reset" 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 different uses of the "git reset" command and demonstrates how to use "git reset soft" to walk back a commit and keep the changes in the working tree and index. He also shows how to use "git reset hard" to completely destroy changes in the working tree and index. Using "git commit --amend" to retroactively apply current changes to the previous commit is also discussed in this lesson.


Transcript from the "Reset" Lesson

>> Alright, Git reset. So this one you've seen me use a couple times because we definitely didn't have any problems earlier which required me to change history or anything. We didn't have any deviations from this course, it's been first try all day long. But if I did, I would have probably used Git Reset.

There's actually quite a bit you can do with Reset. I only use it pretty much one way, which is just get rid of my current changes. That's pretty much what I use it. I don't like these, just get the hell outta here, right? That's what I, that's how I use it.

And so we're not there right now. We're gonna do a little bit different. We're gonna kinda show you a little bit more. So a soft reset is a reset. In which you actually walk back a commit, your branch will walk back a commit with you and that commit that you just effectively forgot and changed history will become the change inside the working tree and index.

So,we just got done reverting E. If I go git reset At soft head tilde one, I'm now gonna walk back that commit. It's still trunk, right? We're still in trunk, but if I go git log one line, you'll notice, where's the revert? The revert. Where's the revert? It's gone, right?

If I go git status Well, look what the VCS drag in, git diff stage. There we go. Awesome. There's the minus C, so you can see I just undid the commit. That's git reset soft, which means that all the changes now go into your working tree slash into your index.

So awesome. Yeah, I actually really liked this, it's really great when you say you're teaching a class and you totally mess up, you can actually walk these things back pretty easy contain all the changes and know how to work things I really liked I actually really liked reset soft and the best part is I didn't really even know about reset soft for a very long time.

Typically what I would do is I would just go into the logs, check out the specific SHA, and then just delete my branch and create that as my new branch. But instead, with reset, hard or soft, you can just walk it back and you don't have to do that little hopping, recreating, checking out, destroying type thing.

There's also kind of like a inverted version of this. Like right now, if I do this reset. Soft. I can actually commit. Right? A new shop, right? And now I have the same change. But I have changed the message so it no longer says we reverted E and it has a new shop.

So I've kind of hit history by accident, right? But I could do the exact same thing in some sense by using git amend. So commit. Dash dash amend allows you to take your current changes and retroactively apply them to the commit you're on. And then you can even do amend dash dash no edit and that will edit the commit you're just on, but we don't have to change the commit message.

And so when i'm doing ci I literally echo a c Space to the end of a file, git add, git commit, amend, no edit, get force push. That way there's only one change. Instead of the CI thing where you have 9,000 changes. You can do a git reset soft git commit and just keep on changing the thing over and over again or just add a space Space and do that.

So amend, reset soft, fun little fun little move here. You know I do it all the time, good times. But remember, you're always changing history when you're doing those things so can be a little more dangerous, you know? All right, so hard. We can do the same thing as soft except for this one drops the changes to index in your working tree.

That means when you go Back, it will completely destroy it. So we'll do the exact same thing. We are going to make a little bit of a change and we're gonna destroy our changes with reset hard. So first, make a change to read me and then add a new file called

Make those two and then let's destroy them. So I'll go vim README, jump in here, there we go, a little bit, Change and then There we go, some Dvorak in there. Get status. You can see these two things right here. Remember, this is a tracked change that's not staged because remember read me as tracked.

It's a part of the working tree. Foo is not a part of the working tree. It has never been tracked. It's never been staged. And so we have no information about it. So if I go git reset hard, it will destroy our working tree changes and index changes.

So if I go git status, I still got foo. I just don't have the readme change anymore. Because remember, it just destroys only what git knows about. Now, here's the danger. This is like the big danger. Where people fall into. If I go git add this, and so now it now knows about this for the first time and I go git reset hard, it's gone and there's like no coming back from this one.

It's a goner, right? You just erased a file that was never tracked, and so there's no previous history at this point. So you gotta be a little careful about using reset heart. You can also do the whole walking back. So if I go up, if I go vim read me and I add a commit, right here and go git add git commit.

Now a bunch of nonsense and then go get a reset. Hard head one. It does the exact same thing that soft did except for again, what does it do to the index and working tree Get rid of it. So, if I go git status, nothing there. It just simply walked me back one commit.

Git log one, you'll see that I'm at the new shot one. So kind of neat. So when you're needing to kind of just save yourself a little bit, knowing about the Git reset hard and soft is pretty nice.
>> It's not really a new show. Go. Anymore? The commitment is it says a new shot but it's get out.

[LAUGH] . That's it. We are fistfighting. All right. So there you go. Pretty fun times. I don't know. I actually really like, it's absolutely fantastic having these. I can't tell you how many odd times that this actually is Useful that I used to do a bunch of steps to do the same thing.

Now it's just like I can just do them in one single command. Because often I find myself like checking out and destroying the past and rechecking out a new branch and doing all this just to walk back a couple commits and I was, ooh, I really goofed, whatever that was.

We should have here, let me revert also the revert. So I'm gonna go like this, git reset hard. There we go. We're back now. We're kinda into this point, now the reverts gone, so we're just right here. Can you restore get hello to the position it was before we started this section.

>> Dip into the ref log, is the SHA the same though or will be?
>> The answer it's reflog. It's always reflog, right? You just gotta go find that nice little ref log. I had a whole bunch of entries here. I did a bunch of extra work. Yeah, you can effectively be able to grab out.

Our previous commit was the river II remember? We did all that so we got to go find the river e so I can go in here and go get ref log. Find wherever revert E is there we go. It's right here. And you can do that. I mean, there's plenty of ways you can do this.

I'm sure there's some much more awesome way to do this, but I can get checkout that thing. I can get branch, delete trunk, I can get checkout dash v trunk. Right? Well, I just restarted from doing that. I'm sure there's some fancy nicer way to do that. I always find myself in habit of just doing that because I'm always I don't know You know Once you do something, then it's really hard to break that habit.

That's typically how I'll always do it. Even with reset soft, sometimes I'll just forget that I can do this big hopping thing and it's beautiful. You can do a get reset hard. Yeah, you can do a get reset hard whatever end. Remember it's a reset hard and then you can go anywhere.

I can actually reset hard Back to a branch that's remote or to anything that's a commit so I could have also done the whole head and then whatever the index was whatever it was 14 Right and I could go off and do that so it's it's a nice little thing

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