Git fundamentals
Theory: Cancelling commits
Git is a system that encourages you to not be afraid of making mistakes. You can even say that it's the only way to learn how to use it. In git, there is almost always a way to restore or change any commits. As a last resort, another git clone will save the day.
What if a commit has already been made, but we don't like it for some reason? There can be many potential situations, and they all arise regularly, even for professional developers:
- Maybe you forgot to add some necessary files to the commit
- Changes need to be rolled back in order to be refined
- The changes are no longer required and need to be deleted
- The changes were made by mistake and need to be reverted
Git is, for the most part, a "forward only" system. The right way to work with git is to create a new one, not change an old one. All of the situations described above can be solved by a new commit with required code changes. It's both safe and convenient. Changing the commit history is a dangerous operation that may cause synchronization problems with remote repositories. We'll talk about that later.
Despite what we said above, there are special commands within git to make it easy to undo or change a commit. With their help, you can make the history of commits clear and make rolling back fast.
Git Revert
The easiest thing to do is to undo the changes. In fact, it boils down to creating another commit that performs the opposite changes to the commit being canceled. It's hard to create this kind of commit by hand, so they added a command to git to automate the rollback. This command is called git revert:
The revert command can "undo" not only the last commit, but also any other commit from the project history. You have to agree, it's very cool. Without the version control system, you couldn't even dream of something like this.
Git Reset
Sometimes you need to delete a commit you only just made by mistake. Of course, in this case, git revert is also useful, but it doesn't do much good to the system if you do that. If the commit was just made now and has not yet been submitted to GitHub, it's better to make it look as if the commit didn't exist at all.
Git allows you to delete commits. This is a dangerous operation that you should only do if it's a new commit that nobody else has except you.
If a commit has been sent to an external repository, such as GitHub, you should never change the history, it will break work done by other people working on your project.
To delete a commit, use the git reset command. This is how it's done:
git reset is a powerful command with many different flags and ways of working. It can be used to delete or undo (without deleting) commits, restore files from the history, and so on. This is an advanced git tool, but here we're only talking about the basics.
The --hard flag means complete deletion. Without it, git reset will undo a commit, but it will not delete it, it will put all the changes to that commit into the working directory so you can continue working with it. HEAD~ means "one commit from the last commit". If we wanted to delete the last two commits, we could write HEAD~2.
HEAD means the last commit made. We'll cover this terminology in more detail in the lesson on the inner workings of git.
If the --hard flag is not specified, the --mixed flag is implied by default. In this case, reset sends the changes of the last commit to the working directory. They can then be corrected or undone and a new commit made.
The last commit no longer exists, but the changes made to it are still there. They are in the working directory for further modification.

