Reverting 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:
Here is its hash:
1b00736255dca7d78659a9971d0c30fba0eb3075
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.
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:
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.
Once again, the default text editor opens with the default commit message for the revert. Leave the message unchanged.
Next, close the text editor properly and display the changes made in 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:
The recipe.txt file has been successfully deleted.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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
Reverting 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:
Here is its hash:
1b00736255dca7d78659a9971d0c30fba0eb3075
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.
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:
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.
Once again, the default text editor opens with the default commit message for the revert. Leave the message unchanged.
Next, close the text editor properly and display the changes made in 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:
The recipe.txt file has been successfully deleted.
Thanks for your feedback!