In the last video we saw how to reset a file or directory to it's last committed state, but what if you make a commit and then need to undo it?
At some point you will make a commit, and then realize that it broke something, or the code just really wasn't ready to be committed. There are two commands that can do that for you "git reset" and "git revert". We'll look at "git reset" in this video.
Make Changes to Files
I'll start out by updating the Views module 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. We can use the "git reset" command to reset our repo to a specified state.
There are a number of options you can pass when using the "git reset" command. You can find the full list in the Git documentation, but I'm just going to show you two at this time. The basic "git reset" and "git reset --hard".
When you use either version, you have the option to pass a commit hash to the command. You can get the hash of the desired commit by typing "git log --oneline". The seven character hash at the beginning of each line in that log is unique to and represents that commit.
- The default reset command is "git reset " (where defaults to HEAD, which is last committed state)
- Use this command when you want to remove the last x commits (back to the hash you choose) but keep changes to files in tact. ie: when you realize you weren't done making your changes and committed too early.
- The other reset command we'll look at is "git reset --hard " (again, defaults to HEAD, which is last committed state)
- This removes the last x commits (back to the hash you choose), and resets changes to files. Use this when you want to completely reset changes and history back to a specific commit.
Let's see these in action.
Commit Views Update
I'll commit the update to views so that we can reset it.
- git add .
- git commit -m "Updated Views to 3.10"
Typing "git log --oneline" shows the new commit, while "git status" shows that we haven't yet pushed the commit to the central repo. This is very important! Once you've pushed a commit, you generally don't want to reset it with the "git reset" command. Instead, you should use the "git revert" command that we'll discuss in the video Reverting One or More Specific Commits with "git revert".
Undoing the Last Commit
Now that we know the commit is only on our machine, let's say we realized that Views 3.10 conflicts with some custom code we've written, and want to revert back to the old version until we can update our custom code. To do that we'll use the "git reset" command and pass the hash of the commit that we would like to reset to.
* git reset 269bec4
You'll see a list of unstaged changes after the reset, "git log --oneline" indicates that our last commit was when we installed Views 3.8, and "git status" shows all of the new and modified files that came with the update to Views 3.10.
Undoing Multiple Previous Commits
You can even use this to go back multiple commits at once. I'll commit these files in two commits, and then show you how to go back more than one commit.
- git commit -am "Updated Views to 3.10"
Now a "git status" show us that we missed the unstaged file. I'll commit that now.
- git add .
- git commit -m "Added missing Views 3.10 file"
Now, "git status" shows a clean working directory, and "git log --oneline" shows the two commits. If you'd like to reset those commits so that you can make one clean commit, you can do that by specifying the commit you would like to reset to.
* git reset 269bec4
Now, you'll see a list of the unstaged changes after the reset, "git log --oneline" indicates that our last commit was when we installed Views 3.8, and "git status" shows all of the new and modified files that came with the update to Views 3.10. Now we're set to add all of the files and make one commit instead of cluttering our log with two.