In Git, git revert is a command used to create a new commit that undoes the changes made by a specific commit or commits, effectively reverting the repository's state back to how it was before those changes were introduced. Unlike git reset, which alters commit history, git revert works by creating a new commit that inversely applies the changes introduced by the specified commit(s).
git revert is similar to git reset, but the approach is slightly different. Instead of removing commits, the revert ONLY undoes a single commit by taking you back to the staged files before the commit.
Here’s a basic overview of how git revert works:
Identify the Commit to Revert: Determine the commit(s) that introduced changes you want to revert.
Use Git Revert Command: Execute the git revert command followed by the commit hash or range of commits you want to revert.
Resolve Conflicts (if any): Git may prompt you to resolve conflicts if the revert conflicts with subsequent changes in the repository. Resolve conflicts as needed and continue the revert process.
Commit the Revert: Once conflicts are resolved (if any), Git will open a text editor for you to provide a commit message for the new revert commit. Save and exit the editor to complete the revert process.
git revert is useful when you want to undo commits that have already been pushed to a remote repository. It is a safe option because it does not destroy any commits and leaves a clear record of the changes made in the repository.
Note: git revert can only directly undo commits that have not been merged into other branches. If you need to undo commits that are part of a merged branch, consider using git reset or another method appropriate for your workflow.
The basic syntax for git revert is:
gitrevert<commit>
You can also revert multiple commits by specifying their hashes:
gitrevert<commit1><commit2>...
<commit> can be a single commit hash, a branch name, or a range of commits (specified using ..).
It’s important to note that git revert does not erase history but rather adds new commits to undo the changes introduced by previous commits. This makes it a safer option for reverting changes, especially in shared repositories where altering history using commands like git reset can cause conflicts for other collaborators.
gitcommit-m"Added the data script files"[master (root-commit) eae84e7] Added the monitoring script files2fileschanged,0insertions(+),0deletions(-)createmode100644test.pycreatemode100644data.py
5
Revert the commit
If the previous commit was not intentional, revert it:
gitreverteae84e7[master c61ef6b] Revert "Added the data script files"2fileschanged,0insertions(+),0deletions(-)deletemode100644test.pydeletemode100644data.py
This creates a new commit that undoes the changes introduced by the commit with hash eae84e7.
6
Inspect the log
View the commit log:
gitlogcommitc61ef6b4e86f41f47c8c77de3b5fca3945a7c075 (HEAD ->master)Author:Megha<[email protected]m>Revert"Added the monitoring script files"Thisrevertscommiteae84e7669af733ee6c1b854f2fcd9acfea9d4a3.commiteae84e7669af733ee6c1b854f2fcd9acfea9d4a3Author:Megha<[email protected]m>Addedthemonitoringscriptfiles
From the log, you can see a new commit (ID c61ef6b4e86f41f47c8c77de3b5fca3945a7c075) reverses the original commit (ID eae84e7669af733ee6c1b854f2fcd9acfea9d4a3).