Register to get access to 15+ free programming courses with interactive exercises

Cancelling commits Git fundamentals

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:

git revert

# This command needs a commit identifier
# This is the commit that deleted the file
git revert aa600a43cb164408e4ad87d216bc679d097f1a6c
# After this command, the editor will open, waiting to enter the commit description
# Usually the revert message doesn't get changed, so just close the editor
[main 65a8ef7] Revert "remove"
 1 file changed, 1 insertion(+)
 create mode 100644
# the file returns to the project

git log -p

commit 65a8ef7fd56c7356dcee35c2d05b4400f4467ca8
Author: tirion <>
Date:   Sat Sep 26 15:32:46 2020 -0400

    Revert "remove"

    This reverts commit aa600a43cb164408e4ad87d216bc679d097f1a6c.

diff --git a/ b/
new file mode 100644
index 0000000..4b34ba8
--- /dev/null
+++ b/
@@ -0,0 +1 @@
+Haskell Curry

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:

# adding a new commit, which we will immediately delete
echo 'test' >>
git add
git commit -m 'update'

[main 17a77cb] update
 1 file changed, 1 insertion(+)
 # It is important not to do a git push

git reset --hard HEAD~

HEAD is now at 65a8ef7 Revert "remove"

# If you look at the git log, the last commit is no longer there

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.

git reset

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.

echo 'no code no pain' >
git add
git commit -m 'update'

[main f85e3a6] update
 1 file changed, 1 insertion(+)

# Now roll back the last commit
git reset HEAD~

Unstaged changes after reset:

git status

On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

The last commit no longer exists, but the changes made to it are still there. They are in the working directory for further modification.

Do it yourself

  1. Follow all of the steps in the lesson
  2. Change the added text to No code No pain and commit it with a message reading "update"
  3. Upload the changes to GitHub

Аватары экспертов Хекслета

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

From zero to a developer. Refunds in case you won't get a job

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.