Join Kevin Skoglund for an in-depth discussion in this video Demonstrating a soft reset, part of Git Essential Training.
In this movie, I want to demonstrate a soft reset. So, here's the commit log. You can see that my most recent commit was reverting this commit that was right before it. What if we wanted to undo our version, that is we wanted to get rid of this? We could revert it and revert the reversion, or what we want to do here is we want to actually rewind back in time to before I made this reversion back to this point here. So, this is the reference we're going to use. We want to rewind the HEAD back to that point so that it records from there going forward. So, what would that look like? Well, the first thing is whenever I start working with moving the HEAD pointer around, I always think it's a good idea to open up a new text file, and let's just grab the most resent commits here, we can grab the first several, no big deal, and let's just take those and paste them in here.
So now we always have these commits to refer to. If we need to get back here, we have that value recorded. Once we rewind, our log won't show it to us anymore. I'm just going to take that and put it out of the way for now. Let's first of all take a look at what the HEAD pointer points at now, git/HEAD. It points to refs/heads/master, and we saw before that, that's also a file, refs/heads/master, and it will always point to that file. So, if we ask for this, as long as we're on the master branch, it will tell us this answer, the real SHA that we're looking for is contained in this file.
So, right now it points to that 5c86ebd. If we take a look here, you can see 5c86ebd is in fact the most recent commit. So now what we want to do is a soft reset, git reset --soft, and we want to use that second commit. Again, let's just pull it up. We're going to go back to this one here. I'm going to just copy that, and there we go, we'll paste it in here. Now, it's going to move the HEAD pointer back to that point in time.
It didn't give me any kind of message there, but if I now do that same look at what it points to, refs/heads/ master, you can see it's changed. Now, it points to this other commit. And if I do git log, you'll see that the most recent commit looks like it's that one, the da3866. Now, here is what git soft does. If we do git status, you'll see that we have in our working directory and in our staging area, we have resources.html still in its most recent state, not the change that's in the Repo here, but the changes that we made since then.
This is the reverted file. Let's take a look at the contents to see, git diff --staged, and you can see this is the change we made that reverted. So it took sunglasses, sunscreen, insect repellent and moved them back to the bottom. So, it did not destructively get rid of our changes. Our changes are still here in the staging index and in the working directory. We are now ready to record a new commit if we want to, and it would just record it over essentially where that other commit was before, a lot like if we had our tape recorder, and I was saying we recorded back, recorded over the last 10 minutes of audio again.
We are essentially forgetting about that old stuff that we were doing, and making new commits from here on out. Or what we can do is we can just go back to this most recent commit and change the HEAD pointer to point back to that, git reset --soft, and put the most recent one in there. Now, if we do git log, now our version has come back again, git status, there is nothing to commit. Our working directory is clean. So all we did was basically take the HEAD pointer from pointing at this one, moved it so it's pointing at this one, and then moved it back pointing to that one again.
You can see why git reset with the soft option is the safest and least destructive, because it didn't actually remove anything at all. All it did was moved the HEAD pointer that other commit was still there, and all of our work was still maintained in the staging index and in our working directory. We didn't lose anything. In the next movie let's compare this to the mixed reset.
- 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