Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reverting a Specific Commit | Undoing Changes
Git Essentials
course content

Contenido del Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

bookReverting a Specific Commit

git revert

As we have already mentioned, to revert a specific commit, the git revert command is used. This command creates a new commit that undoes the changes introduced by the specified commit. The basic syntax is as follows:

As you can see, instead of using HEAD as we did for reverting the latest commit, we should now specify the hash of a particular commit (commit ID) that we want to revert (replace <commit-hash> with a valid hash).

More about Hashes

Remember, each commit has its unique ID which is essentially a string of digits (0-9) and letters (a-f), a hexadecimal string. This 40-character string is called hash. Let’s take a look at our latest commit:

Here is its hash:

1b00736255dca7d78659a9971d0c30fba0eb3075

Note

You will have a different hash for this commit for the reason explained below.

The commit hash is computed using the SHA-1 algorithm based on the following information:

  • commit message;
  • date;
  • author;
  • snapshot of the working tree;
  • hash of the parent commit (or commits when there is more than one parent).

If the commit is the first in the repository, then the hash of the parent commit is apparently not calculated.

Since all of this commit information is used for calculating its hash, using hashes as commit IDs ensures the consistency of the repository. Besides, the probability of two different commits having the same hash (the probability of collision) is extremely low, so it’s very unlikely to happen by chance.

Basically, if anything is changed in the commit, its hash will change too. What this means is that in case the data is corrupted for whatever reason, Git can use the hash to identify this.

Note

When amending a commit, the commit ID changes, which is why it's better not to use the git commit --amend command when working with remote repositories.

Example Workflow

Before we decide which commit to revert, let’s take a look at the four latest commit:

We can see that there is a commit where we added the recipe.txt file. Let’s revert this commit by creating a new commit which will undo these changes and essentially delete the file. In my case its hash is the following:

043b634d76a7a7744757350512b6367417c29e0

Your hash for this commit, however, will be different. We can now revert this commit:

Replace this hash with your hash.

Once again, the default text editor is opened with the default commit message for reverting. We’ll leave it as it is.

Next, we’ll close the text editor appropriately and show the changes made in this latest commit:

7 deletions took place meaning that all 7 lines of the file were deleted. Let’s now verify that the file itself was deleted by listing all non-hidden files and directories in our project directory:

Congratulations! We have successfully deleted the recipe.txt file.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
some-alt