Join Kevin Skoglund for an in-depth discussion in this video Reverting a commit, part of Git Essential Training.
- View Offline
In the last movie, we saw how we could amend older commits by crafting a new commit that would undo the changes from the previous commit, and we talked about how we could do that manually by just making the changes to the files or by checking out a previous version of the file and then assembling that together so we can do a commit. But Git also gives us a helpful way when we really want to just undo the changes for a commit completely and totally, we can use the Revert command. So let's take a look with git log. Here is the commit that we're talking about, this first one, Rearrange the items to bring on an outdoor trip.
What the Revert command will do is it will take all of the changes that were there, and it will flip them around. It will do the exact opposite of those changes. So, anything that was added will be deleted, anything that was deleted will be added back in again, and anything that was modified will be changed back to its previous state. It's going to be a complete mirror image of this commit. So, in order to use it, it's real simple. We just have to grab part of the reference to it. Remember, we don't need the whole thing. I'm just going to copy it, and let's use git revert, and then I'm going to put in that S-H-A.
Now, this is going to do it all in one step for us. It's not going to assemble the commit and then wait for us, it's going to go ahead and make that commit. So, I'm going to hit Return, it's going to pop up with the chance for me to edit the commit message, and it's going to do that in TextMate for me. So it pops up, and it says, all right, if you want to edit this message before I commit it, go ahead and do it now. Now, I'm happy with the default that it gives me. Just lines 1, 2, and 3 are going to be part of commit, all that commented out stuff is going to get ignored, and not be part of my commit message.
So, I'm just going to say Save and Close, and then Git will go ahead and do its thing. It went ahead and made the complete commit. So, if I do git status, you can see my working directory is still clean, but git log, and you can see that it did make this new commit here which reverts that old commit. And sure enough if we go and open up resources and take a peek at it, you'll see that down here it's now put insect repellent, sunglasses, and sunscreen back at the bottom of the list. Now, you can pass in the -in option with revert, and then it won't actually do the commit, it will just stage it and then wait for you to actually do the commit yourself.
It will give you the chance to write your own message for it and also make any other modifications you might need to make. Now, git revert works really well when things are simple. We're just making a simple change from A to B, and then we're going to switch it back from B back to A. But what if the changes are really complicated? What if in the meantime other things have changed? What if files have moved perhaps, things have been renamed? It might get little harder to make an exact mirror image commit. Git is going to use a more complex set of rules for how to deal with those changes, and the set of rules it's going to use is what we use for merging.
Merging is a more advanced technique that we'll talk about a little later on, but if you need to revert something complex, you're essentially going to find yourself doing a merge between the current branch and the new set of changes that you're trying to merge into it.
- 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