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

bookReverting a Specific Commit

git revert

To revert a specific commit, use the git revert command. This command creates a new commit that reverses the changes introduced by the specified commit. Basic syntax:

git revert <commit-hash>

Instead of using HEAD (which targets the latest commit), specify the hash of the commit (commit ID) you want to revert by replacing <commit-hash> with the actual hash value.

More about Hashes

Each commit has a unique ID β€” a 40-character hexadecimal string consisting of digits (0–9) and letters (a–f). This identifier is called a hash. Now, view the latest commit:

The latest commit

Here is its hash:

1b00736255dca7d78659a9971d0c30fba0eb3075

Note
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
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 deciding which commit to revert, view the four most recent commits:

Four latest commits

There is a commit where the recipe.txt file was added. Revert this commit to create a new one that undoes those changes and effectively deletes the file. In this example, the commit hash is:

043b634d76a7a7744757350512b6367417c29e0

Your commit hash will differ. Now revert this commit:

Replace this hash with your hash.

Git revert

Once again, the default text editor opens with the default commit message for the revert. Leave the message unchanged.

Default revert commit message

Next, close the text editor properly and display the changes made in the latest commit:

The latest commit

7 deletions occurred, meaning all seven lines of the file were removed. Now verify that the file itself was deleted by listing all non-hidden files and directories in the project directory:

Listing files and directories

The recipe.txt file has been successfully deleted.

question-icon

Match the actions with the respective commands.


Revert the latest commit:

Revert a specific commit:

Unstage changes:

Revert unstaged changes:

Unstage changes and then revert the changes in the working directory:

Overwrite the existing commit:

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What happens if I revert a commit that is not the most recent one?

Can I revert multiple commits at once?

What should I do if I encounter a merge conflict during a revert?

Awesome!

Completion rate improved to 3.57

bookReverting a Specific Commit

Swipe to show menu

git revert

To revert a specific commit, use the git revert command. This command creates a new commit that reverses the changes introduced by the specified commit. Basic syntax:

git revert <commit-hash>

Instead of using HEAD (which targets the latest commit), specify the hash of the commit (commit ID) you want to revert by replacing <commit-hash> with the actual hash value.

More about Hashes

Each commit has a unique ID β€” a 40-character hexadecimal string consisting of digits (0–9) and letters (a–f). This identifier is called a hash. Now, view the latest commit:

The latest commit

Here is its hash:

1b00736255dca7d78659a9971d0c30fba0eb3075

Note
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
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 deciding which commit to revert, view the four most recent commits:

Four latest commits

There is a commit where the recipe.txt file was added. Revert this commit to create a new one that undoes those changes and effectively deletes the file. In this example, the commit hash is:

043b634d76a7a7744757350512b6367417c29e0

Your commit hash will differ. Now revert this commit:

Replace this hash with your hash.

Git revert

Once again, the default text editor opens with the default commit message for the revert. Leave the message unchanged.

Default revert commit message

Next, close the text editor properly and display the changes made in the latest commit:

The latest commit

7 deletions occurred, meaning all seven lines of the file were removed. Now verify that the file itself was deleted by listing all non-hidden files and directories in the project directory:

Listing files and directories

The recipe.txt file has been successfully deleted.

question-icon

Match the actions with the respective commands.


Revert the latest commit:

Revert a specific commit:

Unstage changes:

Revert unstaged changes:

Unstage changes and then revert the changes in the working directory:

Overwrite the existing commit:

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5
some-alt