Sometimes you'll make changes to a file, and then realize that your changes broke something, or for some reason you just want to scrap them altogether. If you've already committed your changes to the repo, this is a little harder to fix, but we will talk about the ways to do that in following videos with "git revert" and "git reset".
In this video we'll take a look at what to do when you realize the issue before you commit the files, which is much easier to fix.
I'll start out by downloading an old version of a module, committing it, and then updating it. Then we'll take a look at how to revert the update to the committed version.
(I'm going to perform the install, "commit" and "push" quickly to save time, but if you're not familiar with the commands I use, watch the video Making a Change to the Local Repository and Pushing it to the Central Repo with "git push" where I go through the process in detail.)
- drush dl views-7.x-3.8
- git add sites/all/modules/contrib/views
- git commit -m "Installed Views 3.8"
- git push
- drush en views -y (I'm enabling Views because Drush is configured to only update enabled modules.)
Okay, now that we have an old version of Views installed, I'll update it to the latest release.
- drush up views -y
Now "git status" will show modified and untracked files. Let's say we've tested the update, and it causes a conflict with another module we're using. So, we want to undo the update. Since we haven't committed the changes, we can use the "git checkout " command to checkout the last committed version of a file.
Reverting to the Committed Versions of Files
Let's checkout the first file "LICENSE.txt"
- I'll type "git checkout sites/all/modules/contrib/views/LICENSE.txt" and press Enter
Now when I type "git status" the LICENSE.txt file is no longer displayed as modified. It is back to the state it was in at the last commit.
You can also use this to checkout an entire directory. Checking out the "sites/all/modules/contrib/views" directory will restore it to the committed version (with one exception. You'll see it in just a second.)
- git checkout sites/all/modules/contrib/views
Now a "git status" shows only one file. It's an untracked file. So this demonstrates that "git checkout" will only affect modified files, not new ones. In this case, we'd just have to delete the new file with "rm -rf " to delete the extra file. I'll do that.
- rm -rf sites/all/modules/contrib/views/modules/field/views_handler_filter_field_list_boolean.inc
Now "git status" shows a clean working directory and "git log --oneline" shows that we're back to the commit where we installed Views 3.8.
There is a way to reset everything including multiple directories with a single command using "git reset". We'll take a look at that in the next video.