A commit
in Git refers to a specific version of a codebase. Sometimes, we may want to undo changes made in a particular commit. To do this, we can use the git revert
command.
The git revert
command is useful in a variety of situations, including:
In these situations, the git revert
command is a safe and easy way to undo changes made in one or more commits without destroying the entire codebase or creating complex merge conflicts.
To revert a single or specific commit, use the following syntax:
git revert <commit-sha>
Where commit-sha
is the identifier for the specific commit you want to revert.
Example:
Let’s say you have made a commit with the following identifier: 56789abc
and you realize it contains mistakes/bug. To revert this commit, use the following command:
git revert 56789abc
This will create a new commit that undoes the changes made in the original commit with identifier 56789abc
.
git revert --edit <commit-sha>
By default, the git revert
command automatically creates a commit message that indicates that the changes made in the original commit have been undone. However, you can also edit the commit message manually using the --edit
option.
This option is useful when you want to add additional information or context to the commit message. It can also be used to follow a specific commit message format or style.
Example:
Let’s say you have made a commit with the SHA abc123
and you want to undo the changes, but you also want to add a custom commit message. You can use the following command:
git revert --edit abc123
This will open the default text editor, where you can edit the commit message. Once you save and close the editor, the changes will be committed.
If you don’t want to edit the commit message and prefer to use the default commit message generated by git revert, you can use the --no-edit
option.
This option is useful when you want to quickly undo changes without spending time editing the commit message. It is also useful when you don’t need to add any additional information to the commit message.
By default, the git revert
command automatically creates a new commit that undoes the changes made in the original commit. However, you can also stage the changes without committing them using the --no-commit
option.
git revert --no-commit <commit-sha>
Example:
Let’s say you have made a commit with the SHA abc123
and you want to undo the changes, but you don’t want to create a new commit yet. You can use the following command:
git revert --no-commit abc123
This will stage the changes, but they won’t be committed yet. You can then review the stage changes and commit again.
git revert -- <file>
This command will undo the changes made to a specific file in the last commit and create a new commit with the reverted changes. The <file>
parameter is the name of the file that you want to revert.
This command is useful when you may want to revert changes made to a specific file instead of the entire commit.
Example:
git revert -- main.js
This will undo the changes made to the main.js file in the last commit and create a new commit with the reverted changes.
Use case:
Revert multiple commits:
git revert <commit1> <commit2> <commit3> ...
This command will undo multiple commits specified by their respective SHA values.
This command is useful when you may want to revert multiple commits that were made in error or that caused issues in your code.
Example:
git revert abc123 def456 xyz789
This will undo the changes made in the commits with the SHA values of abc123
, def456
, and xyz789
.
git revert -m <parent-number> <commit-sha>
This command will undo the changes made in a specific commit, but keep the original commit message. The -m
flag specifies the parent number of the commit to be used as the message.
Useful when you may want to revert changes and keep the original commit message for reference or to provide context for the revert.
Example:
git revert -m 1 abc123
This will undo the changes made in the commit with the SHA value of abc123
and use the original commit message as the message for the revert commit.
If you want to revert a range of commits, you can use the following syntax:
git revert <first-bad-commit-sha>^..<last-bad-commit-sha>
Example:
Let’s say you have made commits with the following identifiers: abc123
, def456
, and ghi789
, and you want to revert the changes made in all three of them. To do this, use the following command:
git revert abc123^..ghi789
This will create a new commit that undoes the changes made in the range of commits from abc123
to ghi789
.
git revert -m 1 <merge-commit-sha>
This command will undo a merge commit
and return the repository to the state it was in before the merge. The -m
flag specifies the parent number of the merge commit to be used as the message.
Useful when you may want to revert a merge that caused issues in your code or that you made in error.
Example:
git revert -m 1 abc123
This will undo the merge commit
with the SHA value of abc123
and return the repository to the state it was in before the merge.
git revert -m 2 <merge-commit-sha>
This command works similarly to the previous one, but instead of undoing the merge commit, it will preserve it and create a new commit that undoes the changes made in the merge. This is useful if you want to keep the merge commit for reference or to provide context for the revert.
Useful when you may want to keep the merge commit for reference, but you may want to undo the changes made by the merge.
Example:
git revert -m 2 abc123
This will preserve the merge commit with the SHA value of abc123 and create a new commit that undoes the changes made in the merge.
The git revert
command is an essential tool in any Git workflow, allowing you to undo changes made in specific commits or ranges of commits. Whether you need to fix mistakes or test different solutions, the git revert
command gives you the flexibility to revert changes in a controlled and ✔️safe manner.