Revert Commit | Bondar Academy
Course: Git and GitHub Fundamentals
Module: Working with Git and GitHub
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to revert changes in Git by using two primary commands: git reset and git revert . These commands allow you to navigate back to previous commits in your Git history. Understanding HEAD The HEAD in Git is a pointer that indicates the current commit. By default, it points to the latest commit, but you can move it back and forth to view different versions of your code. Using git reset git reset : This command, by default git reset --mixed , allows you to move the HEAD pointer to a previous commit without losing any data. The changes after the specified commit become unstaged. git reset --hard : This command deletes all changes after the specified commit. Use it with caution, as it cannot be undone. Considerations for Remote Branches When working with remote branches, you cannot simply reset the origin branch to an earlier commit. Instead, you would need to use git push origin master --force , which is generally not recommended for the master branch due to potential data loss. Using git revert The git revert command creates a new commit that undoes the changes made in a specified commit without altering the commit history. This is safer for collaborative environments as it maintains the integrity of the commit history. Key Points to Remember: Use git reset for local changes where you can afford to lose data. Use git revert to safely undo changes while preserving commit history. Be cautious with git reset --hard as it permanently deletes changes. By mastering these commands, you can effectively manage your code versions in Git.
Video Transcript
All right, sometimes when you messed up something in your code and it doesn't work, you want to revert back in time to the previous working version. And this is what you can do in Git, reverting to the previous commit. And in this lesson, I will show you how to use git reset and git revert to revert the previous versions on the Git branch. All right, let's jump into it. All right, so let's talk about how to revert changes in Git. So here is our master branch with the list of commits that we perform in this class so far, and I would like to introduce a new definition in Git called head. So head in Git is the simply pointer which is looking into the version of the commit. By default, head is always looking into the latest commit, but you can move this head back and forth in order to switch between commits. So if we take this example with testfile.txt, if we move our head a little bit on the left, this is what the text file will look like. Or if we move head one commit before that, this is what commit will look like. This is the test phrase, and this is the second phrase. This is the place where we changed a phrase to word between those commits. So changing the position of the head in the Git branch defined what changes are reflected in your code. So now, how to do this change, how to reset the head? So the first example is the basic one, git reset, or by default, it's called git reset mixed. So every commit, I remind you, have a unique number. It's also called a SHA number. And by providing the SHA number of desired commit where we want to point our head, we can reset the head pointer. So for example, if I execute command git reset and provide the SHA, the head will be moved to this commit. And the changes that were ahead of this commit will be just unstaged. So this is the most safe way of moving the head back in time. So you will not lose any data. Your changes will be just unstaged. So you can modify them, review them, and then commit as a new commit. And then after that, when you create a new commit with your modification, your branch just continue kind of a new direction and head is moving with a new commit. But the old records that were in the branch before, they are remaining in the branch. They are not part of the tree history anymore. But if later for some reason you want to go back to this commit or this commit, you can point head to this particular SHA number to jump into the commit. But the tree history will be a new one following this line. And with each next commit, the head will be moving forward. So another example is git reset hard. When you perform git reset hard with the command git reset hard and SHA number, the changes that were done, they are just simply deleted. So the head is moved to the direction of the desired commit. But other commits that were ahead of this commit are simply deleted and gone. And it is not possible to recover them. So this command should be used very wisely and carefully. And you need to execute hard reset only if you're 100% sure that you want to reset to a particular commit and you don't need the changes that were done, that you can safely lose the progress that you performed. So when you work on the local branch, well, there is no issues with that. You can easily move the head back and forth. But if you are synchronized your branch with the origin master branch and your head is pointing to the same commit, then if you reset head to the earlier commit, unfortunately, you cannot do exactly the same thing on the origin master branch. You cannot, for example, merge the local master branch into origin master branch and that way reset the commit history. The only way to reset origin branch is to execute command git push origin master dash dash force. And of course, this is highly unrecommended approach. And of course, doing this on the master branch. So you can safely perform this operation if you are working on your own branch, then there is no problem at all. You can make force push to synchronize remote branch with your working branch. And then after that, create a pull request to a master branch. In most of the projects, master branch is highly protected and not allowed to perform a force push. So what are the alternatives for that? The second alternative would be to use a git revert command. So this is the same situation. We have five commits, master origin and master local branch head is pointing to the latest commit. And let's say that we want to revert to this commit in history and we can execute command git revert and provide the SHA to the commit. So what this command will do instead of moving head back in time, git revert will create a new commit with a snapshot of the commit that was back in time. So the commit with the SHA-350 will be recreated as a next commit with a new SHA number. And in that way, you can easily push this branch to remote and then create pull request and merge your changes into origin master branch. But there are a few things that you need to know about this approach. When you perform git revert, you do not cancel all the changes between the latest commit and the few commits between them. So these two commits will remain in the branch. Only the changes that were performed in this commit will be restored as a next commit into the branch. And the second thing, if in these two commits you was modifying the same code that you want to restore, most likely you will have a merge conflicts that you will need to resolve. So this is something that you need to be aware of. And let me show you right now all those examples in the action. So this is our repositor with test project. And here we can see that current master branch has total of seven commits. So let me click on that. And here is the list of all commits that we have performed, which I just show you on presentation. Initial commit, create a text file. This is a second phrase, modified phrase, one pull request, edit new text line and another pull request. And if we click on the commit SHA right here, we can see what was changed. So I click on here and we see that in this commit, we replaced phrase with the word. And if I click on the next one, so we can see that we added this line and we added this is a second phrase. And let's say I want to revert back in history to this version of the text file and I can do it using git reset. OK, so going back to Visual Studio Code and let me show you first how to make a regular git reset. First, let's list the list of our commit. I type git log and to make it more compact, I type dash dash one line and hit enter. This is the list of our commits. Currently, we can see that head is pointed to the master branch to this SHA commit and origin head is also at the same commit. And I want to revert my head to this particular commit. So I will revert the changes back in time and I will type command git reset and take the SHA of this commit and hit enter. And now look what we have. So nothing is changed in this file, but we can see that it is highlighted in blue. And if I navigate here to the source control, we can see that test file is on stage at this point. So if I click on this now and we see these changes. So this is a change that is currently at this SHA and this is on the right, the changes that are not committed. So mixed reset allow us to review the changes before we want to perform any other modifications. So if we look at this change and we hundred percent sure that, yeah, we want to go back in time, then we can simply click on this discard changes button over here and all the changes that were performed to the text file will be discarded, leaving us only with the test file at this particular point of time. And also look down here in the bottom. Git identified immediately that our remote master branch is fork commits ahead of the current master branch. So it means that master branch has a newer changes than our local host master branch. And if I will click discard the changes. So this is the current state of the SHA that our head is pointing to. But if I click on the button synchronize, now we pulled back all the changes from the remote repository, pointing back the head of the local branch to the head of the remote branch. And the second example is to reset to the same exact commit, but with heart. I provide dash dash, heart, enter. And now we don't see anything that is on stage. Files locally were changed immediately and there is no way we can go back in time working on this particular branch. The changes that were in the future, they are just gone. The only way how we can recover the information is just synchronizing again with the remote repository and pulling the changes from remote master branch, recovering all the information that we have. And let me do this. I'm clicking on synchronize and we have all these changes back. And now let me show you another situation when, for example, we want to go to this SHA. We want to recover this second phrase, but then we want to merge those changes safely into the master branch. And using git revert would be a best approach for that. So let me create a new branch for that. Create a new branch, revert phrase, for example. Now I will perform git revert and provide SHA of the version of the file that I want to revert and hit enter. And we expectedly have a merge conflict because the file was modified after that. And the version of the commit that we're trying to recover in the master branch does not like these changes. And we need to recover this. Like a Visual Studio code has a very nice interface resolving a merge conflict. So let's do this. I click on resolve in merge editor. So here on the left we see is incoming change from the our incoming commit and current. This is what the latest changes that we have in the current commit. And in the bottom, this is the final result that we are looking for. And looks like this is exactly what we want to see. This is the test phrase and this is the second phrase. This is how we expect the test file should look for. So we just complete the merge. And now we need to update our commit message. So let's say revert. This is a second phrase commit and we can push the changes to remote repository. All right, now going back to remote repository. And we can perform a pull request to make those changes. Create pull request. No merge conflicts. Confirm merge. And delete the branch. Going back. Now we can see that test file has the view that we expected. So we successfully reverted this file to its initial state. And we also see the commit message revert. This is a second phrase. Now we can go back to a master branch in our project. And synchronize remote repository with the local repository. Okay, master branch is synchronized. We were successfully reverted to the previous commit. All right, so this is two main approaches how you can revert back in time to previous commits. With command git reset, you point the git head to the previous commit in the branch. And using git revert, you can take commit in the past. And restore this commit as a new commit on the current branch. All right, that's it guys and see you in the next lesson.