Video: Demonstrating a hard reset

In the last two movies we've taken a look at a soft reset and a mixed reset. In this movie we're going to take a look at the hard reset, or a Git reset using the hard option. Now, of the three this is the most destructive, because all three of them rewind the HEAD pointer to point to another commit, but the other two leave the files either in our staging index or in our working directory so that we then have those changes still at hand ready to remake those commits. The hard reset doesn't do that.

The course shows how to use Git, the popular open-source version control software, to manage changes to source code and text files. Using a step-by-step approach, author Kevin Skoglund presents the commands that enable efficient code management and reveals the fundamental concepts behind version control systems and the Git architecture. Discover how to track changes to files in a repository, review previous edits, and compare versions of a file; create branches to test new ideas without altering the main project; and merge those changes into the project if they work out. The course begins by demonstrating version control in a single-user, standalone context, before exploring how remote repositories allow users to collaborate on projects effectively.

Demonstrating a hard reset

In the last two movies we've taken a look at a soft reset and a mixed reset.In this movie we're going to take a look at the hard reset, or a Git resetusing the hard option.Now, of the three this is the most destructive, because all three of them rewindthe HEAD pointer to point to another commit, but the other two leave the fileseither in our staging index or in our working directory so that we then havethose changes still at hand ready to remake those commits.The hard reset doesn't do that.

Hard reset makes our staging index and our working directory exactly match the repo.It throws out everything that happened after that.Those commits are not just sitting there waiting for us to recommit, we're nowessentially rewound back to that previous commit.That makes it the easiest way for you to lose data.The main time that you want to use a git reset hard is when things have reallyjust gotten out of hand in your working directory, they're just completely outof sync, that's what git reset hard is great at.It just says, you know what, I don't want everything that happened after that.

I really wish I could go back and just do a hard reset to this point and thenmove forward from there. So let's try it.Again, just like on the other two, grab some of these commits and put those intoa new text file, so then we'll have these references to these later commits ifwe decide that we want to go back to them.And the commit that we're going to roll back to is going to be the same one,we'll just go back to that right there.So it's git reset with the hard option to this SHA right here so that's going torewind back and undo the revert commit that we made.

So it tells us HEAD is now at this point, and it gives us the name of that.If we take a look at the log, we'll see that in fact that's what it points to,that's the most recent commit.And if we do git status, you'll see that it tells us our working directory isclean, there's no trace of that file.There is no trace of the changes that we made to resources.html.Now, we can at this point do our revert again if we wanted to, if we wanted todo it over, or if we just thought we wanted to do something different, we've nowessentially rewound our project back to this previous point in time.

I said that it throws everything away, that's not entirely true, it doesn'tthrow everything away, it's just not sitting here waiting for us to make thosecommits, it's not at hand.However, those old commits are still there, we can still move our HEAD back tothis later point in time.So let's do that, git reset --hard, and we'll put in the later commit again, itnow moved it up there, that git object was still there, it's still sitting therein the git folders, it had just moved the HEAD pointer away from it.

At some point if we hadn't done anything with it, it would have gotten garbagecollected and thrown away, that would have been a long ways down the line.It would have hung on to it for a while just to make sure that we didn't want to go back to it.But it wouldn't have been easy to go back to it if we didn't record this reference right here.We wouldn't have those changes in our working directory, and we wouldn't knowthe name of the commit that would take us back there to get to it.So unlike the other two examples that we did with reset, let's go ahead and takethe additional step of making a new commit here.

So let's do git log, and instead of just reverting this commit, let's rewindback here before we even rearrange the items the first time.So I'm going to use this 2907 as the SHA, let's do git reset --hard, and let'srewind back to that point. So now it's rewound us back to git log.You can see we're back to removed contractions from page text, git status, youcan see that there's nothing in my staging index or working directory.And if we open up resources.html, and we scroll down to that list, you'll seethat it's back in its original state.

So it essentially did the reversion by allowing us to rewind.Let's say that we now want to make additional changes though and go forward.Let's say that we want to just move sunglasses up here after hat, put it rightin front of hat, and then I'll save it, close it, git status. Let's do gitcommit, and we're going to use the -a option, which is going to commit all of mychanges all at once with the message, and we're going to say "Moved sunglasseshigher in list of suggested outdoor items".

So now it's made my new commit. Nothing in my directory. Git log.Now I've put a new commit here.So now, after this one comes this one, I've essentially rewound in time and started recording.And now my HEAD pointer points to this new commit, and all my future commit will just come off of that.Those other commits that we made, they're now just lost, they're sitting thereabandoned in the git folder and they will eventually get garbage collected.So that's how you use the three different forms of reset, soft, mix, and hard.

Again, just be careful when you use them, because they do allow you to overwrite data.

Find answers to the most frequently asked questions about Git Essential Training .

Here are the FAQs that matched your search "" :

Expand all | Collapse all

please wait ...

Q: In the Chapter 10 movie "Configuring the command prompt to show the branch," when I type the function "__git_ps1," I do not get the expected result.

Q: When I use the code the instructor advises in the above video ("git config --global user.name "Nelda Street"), I still get an "Illegal Instruction" error. I have OS 10.6.8. Am I doing something wrong?

A: The current installer version of git isn't compatible with older Mac OS versions.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.