Join Kevin Skoglund for an in-depth discussion in this video Demonstrating a hard reset, part of Git Essential Training.
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.
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 now essentially 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 really just gotten out of hand in your working directory, they're just completely out of 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 then move forward from there. So let's try it. Again, just like on the other two, grab some of these commits and put those into a new text file, so then we'll have these references to these later commits if we 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 to rewind 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 is clean, 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 to do it over, or if we just thought we wanted to do something different, we've now essentially rewound our project back to this previous point in time.
I said that it throws everything away, that's not entirely true, it doesn't throw everything away, it's just not sitting here waiting for us to make those commits, it's not at hand. However, those old commits are still there, we can still move our HEAD back to this later point in time. So let's do that, git reset --hard, and we'll put in the later commit again, it now moved it up there, that git object was still there, it's still sitting there in 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 garbage collected 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 know the 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 take the additional step of making a new commit here.
So let's do git log, and instead of just reverting this commit, let's rewind back 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's rewind 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, you can 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 see that 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 right in front of hat, and then I'll save it, close it, git status. Let's do git commit, and we're going to use the -a option, which is going to commit all of my changes all at once with the message, and we're going to say "Moved sunglasses higher 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 there abandoned 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.
- Exploring the history of version control
- Installing Git on Mac, Windows, and Linux
- Initializing a repository
- Writing useful commit messages
- Understanding the Git three-tree architecture
- Tracking when files are added, edited, deleted, or moved
- Viewing change sets and comparing versions
- Undoing changes and rolling back to previous versions
- Ignoring changes to select files
- Creating and working with code branches
- Merging branches and resolving merge conflicts
- Stashing changes for later
- Working with hosted repositories and remote branches
- Developing an effective collaboration workflow
Skill Level Beginner
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.
A: The function "__git_ps1" was recently moved to a new file, .git-prompt.sh, as described here: https://github.com/git/git/commit/af31a456b4cd38f2630ed8e556e23954f806a3cc.
We will update the video. In the meantime, you may do the same steps you do for .git-completion.bash, but a second time using ".git-prompt.sh" as shown here: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh.
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.
1. To add "-mmacosx-version-min=10.6" as described here:https://stackoverflow.com/questions/14268887/what-is-the-illegal-instruction-4-error-and-why-does-mmacosx-version-min-10https://stackoverflow.com/questions/10177038/illegal-instruction-4-shows-up-in-os-x-lion
2. Or to use the version of git that comes with Xcode, or to use homebrew to install git instead.http://superuser.com/questions/697144/installed-git-not-sure-how-to-get-it-working